Ammar Khan
Ammar Khan

Reputation: 2585

Breeze - How to handle the state of two entities in the same view before save changes call

I have 3 entities Project, Attachment and Messages. Project can have multiple attachments and messages.

The problem is on the project detail view, I am listing it's messages and it's attachment (if any) or allow user to add new attachment and post new message. Here is a snapshot for my view

https://www.dropbox.com/s/9dfddv9oa7wo8rk/view.PNG

As soon as the user Add new attachment, new attachment entity created through the manager and after the file get's uploaded, save changes immediately called to the server. But before the project detail view render I also created a new entity for message which throw validation error when attachment entity created and save changes called and it make sense. So, how would I handle the state of these two entities? Here is my code

Model Classes

Project.cs

Public class Project
{
     public int Id { get; set; }

            [Required, MaxLength(50)]
            public string Title { get; set; }

            [Required, StringLength(2000, ErrorMessage = "Enter {0} between {1} to {2} characters long", MinimumLength = 300)]
            public string Description { get; set; }

            [Required, Range(10, 10000)]
            public int Budget { get; set; }

            [Required]
            public DateTime Created { get; set; }

            public ICollection<Attachment> Attachment { get; set; }
            public ICollection<Messages> Messages { get; set; }

}

Messages.cs

 public class Messages
    {
        public int Id { get; set; }
        public int ProjectId { get; set; }

        [Required]
        public string Text { get; set; }
        public Project Project { get; set; }
    }

Attachment.cs

 public class Attachment
    {
        public int Id { get; set; }
        public int ProjectId { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
        public Project Project { get; set; }
    }

Controller.js

 function activate() {

                getNewMessage();
                common.activateController([getProject()], controllerId)
                    .then(function () {

                    });
            }

     function addNewAttachment(file) {

        var attachment = datacontext.createNewAttachment();
        //other stuff

        return datacontext.save()
            .then(function () {


            });
    }



  function getNewMessage() {
            vm.message = datacontext.createNewMessage();
        }

datacontext.js

  function createNewMessage() {

        return manager.createEntity('Messages');
    }


    function createNewAttachment() {
        return manager.createEntity('Attachment');
    }

Upvotes: 3

Views: 238

Answers (1)

PW Kad
PW Kad

Reputation: 14995

If you want to create the attachment whilst not saving the message you can do so like this -

function attachOnly(attachment) {
    // Put the attachment in an array to pass to your entity manager
    var entitiesToSave = [attachment];

    // em is your entity manager
    em.saveChanges(entitiesToSave).then(function(saveResult) {
        // save was successful
    }).fail(function (e) {
        // e is any exception that was thrown.
    });
}

Essentially this going to call save changes only on your attachment object and ignore the validation errors from your message.

Upvotes: 2

Related Questions