Based on saabeilin's answer and Backbone's annotated source, I came with an optimal function to fill the need for resetting a model.
Reusable reset
/**
* Clears the model's attributes and sets the default attributes.
* @param {Object} attrs to overwrite defaults
* @param {Object} options to pass with the "set" call.
* @return {Backbone.Model} this object, to chain function calls.
*/
reset: function(attrs, options) {
// adds default option reset to true
options = _.extend({ reset: true }, options);
// ensure default params
var defaults = _.result(this, 'defaults');
attrs = _.defaults(_.extend({}, defaults, attrs), defaults);
// apply
this.clear({ silent: true }).set(attrs, options);
// triggers a custom event, namespaced to model in order
// to avoid collision with collection's native reset event
// when listening to a collection.
if (!options.silent) this.trigger('model:reset', this, options);
return this;
},
The following line ensures that even if an attribute is passed as undefined { test: undefined }
, it'll still have its default value.
attrs = _.defaults(_.extend({}, defaults, attrs), defaults);
Here's an example:
var defaults = { test: 1 },
attrs = { test: undefined };
_.extend({}, defaults, attrs); // {test: undefined}
_.defaults(_.extend({}, defaults, attrs), defaults); // {test: 1}
Extending Backbone
And if you want it with all Backbone models, you can extend Backbone itself:
_.extend(Backbone.Model.prototype, {
reset: function(attributes, options){
/* ... */
}
});
Disclaimer: Don't do this if you're writing a JavaScript lib or Backbone plugin, as it could collide with another lib or it could cause a different behavior than the one expected by the person using your code.