3

I'm using Backbone in a user settings profile form. I fetch the model and render its information into the form. When I save the model all the parameters are sent instead of the ones changed by the user.

First I fetch the mode and render it on the view:

// Fetch model and render on view
user = new User({ id: 123 });
user.fetch().done(function () {
    view.render();
});

When the user changes a field I updated the model with the 'set()' method:

"change .email": function () {
    this.model.set("email", this.email.val());
}

Later in the view code I save on a click event (using patch: true):

"click .save": function () {
    console.log(this.model.changedAttributes());  // Shows all the model attributes
    this.model.save({ patch: true }); // Sends all the model attributes
 }

How can I avoid Backbone marking as changed after the fetch I use to initialize the model?

eliocs
  • 18,511
  • 7
  • 40
  • 52

1 Answers1

1

This hack will solve your problem:

model.fetch({context:model}).done(function () {
  this.set({});
  view.render();
});
Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55
  • @eliocs because here https://github.com/documentcloud/backbone/blob/master/backbone.js#L322 `changed` property sets to `{}`. Because `attrs === {}` we skip this loop https://github.com/documentcloud/backbone/blob/master/backbone.js#L331 – Vitalii Petrychuk Jan 23 '13 at 18:37