Easiest way to reset Backbone's model to initial defaults?
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.
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());