3
votes

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?

1
Can you please show how you update your model?Vitalii Petrychuk
@VitaliyPetrychuk I don't it's relevant but I included it anyway.eliocs

1 Answers

1
votes

This hack will solve your problem:

model.fetch({context:model}).done(function () {
  this.set({});
  view.render();
});