psixdev
psixdev

Reputation: 151

Can I save changed attributes of model after several set

I call set method multiple times and change several attributes. Then I want to send the changed data to the server with {patch: true}.

I can use model.save(attrs, {patch: true});, but I do not know attrs. I can't use model.toJSON() (unneeded fields) or model.changedAttributes() (only last set) to obtain attrs.

How can I do this?

Upvotes: 2

Views: 842

Answers (3)

Emile Bergeron
Emile Bergeron

Reputation: 17430

While TJ has the right answer, I have a better way to achieve what he suggest.

Instead of making a raw clone of the attributes, I prefer to keep a master copy of the model at the start of the app or the initialize of the view.

this.master = this.model.clone();
// ... then changes are made to this.model

When ready to save, use the master model to compare the attributes and retrieve the changes directly.

var changes = this.master.changedAttributes(this.model.attributes);
if (changes !== false) this.model.save(changes, { patch: true });

With this, you can skip the dataTosend = model.pick(_.keys(changedAttrs)) altogether since changes already is an object of all the differences with the initial state of the master model.

If it's a view that is re-used after the model save:

var View = Backbone.View.extend({
    initialize: function() {
        this.updateMaster();
    },

    saveChanges: function() {
        var changes = this.master.changedAttributes(this.model.attributes);
        if (changes !== false) {
            this.model.save(changes, {
                patch: true,
                context: true,
                success: this.updateMaster
            });
        }
    },

    updateMaster: function() {
        this.master = this.model.clone();
    },
});

Upvotes: 0

T J
T J

Reputation: 43156

According to changedAttributes:

Optionally, an external attributes hash can be passed in, returning the attributes in that hash which differ from the model.

So you could try caching the state of model using toJSON before you start modifying. Once your modifications are done, pass the new state to changedAttributes method to retrieve changed attributes hash and then send a patch request. Something like

var oldAttrs = model.toJSON();

// ...do modifications here

var changedAttrs = model.changedAttributes(oldAttrs);
dataTosend = model.pick(_.keys(changedAttrs));
model.save(dataTosend, {patch: true});

Upvotes: 2

Kiran Shinde
Kiran Shinde

Reputation: 5982

Bind a listener for model

If you are setting value in view, listener should be like (better write it in initialize function)

  this.listenTo(this.model, "change", this.onModelValueChange);

And your listener function

  onModelValueChange: function(model, args) {
      model.save(args.changed, {patch: true});
  }

Upvotes: 0

Related Questions