Easiest way to reset Backbone's model to initial defaults?
Solution 1:
myModel.clear().set(myModel.defaults);
Solution 2:
I came up with the following approach:
reset: function () {
this.clear({silent: true});
this.set(this.defaults);
}
Having {silent: true}
in the clear()
call ensures that the change
event is not fired; it will be fire only on set()
call.
Solution 3:
I do this when the model has non-null initial object properties.
first, define defaults as a function
var MyModel = Backbone.Model.extends({
defaults:function(){
return {
AnArrayTypeProp:[]
};
}
});
second, when needed to reset model to default
model.clear().set(model.defaults());