I am doing something like this in my code:
data_hash = {};
data_hash['some_model_key'] = 'A';
console.log(data_hash['some_model_key']); /* prints A */
model.save(data_hash,{
wait:true,
success:function(){
console.log(data_hash['some_model_key']); /* prints B */
}
});
I understand that if the server changes the state of the model and i am setting wait:true then my backbone model should receive the new value. But why is my attribute hash being changed?
Why Backbone changes your object
If you take a look at the annotated source code, you’ll notice that in case of a
wait:trueoption, the success callback extends the attr object with the server attributes.And according to Underscore doc, _.extend copies all of the properties in the source objects over to the destination object, overriding any previously defined property. Why it is the chosen behavior is a guess, but I suspect it is to keep all references in sync with the “real” state of the model. Or it’s an unforeseen side effect.
Note that the first object you pass to
model.saveis expected to be attributes you want to set on your model as part of the save process. From Backbone docWhat you can do to keep your object untouched
Pass a clone of your object to
model.save:Note: you don’t need to have a
wait:trueoption for your model to receive the values from your server,model.setwill be always be called with the new values.