2

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?

T J
  • 42,762
  • 13
  • 83
  • 138
psixdev
  • 131
  • 1
  • 8

3 Answers3

2

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});
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
T J
  • 42,762
  • 13
  • 83
  • 138
0

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});
  }
Kiran Shinde
  • 5,732
  • 4
  • 24
  • 41
  • 1
    But I want to call `save` only once – psixdev Jan 03 '17 at 09:46
  • I think that it is necessary to create issue on the backbone github... To my opinion. I think it would be good if `save` without attrs on first parameter save all changed attributes. – psixdev Jan 03 '17 at 09:54
0

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();
    },
});
Community
  • 1
  • 1
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129