Skip to content

Instantly share code, notes, and snippets.

@moos
Last active September 17, 2015 17:37
Show Gist options
  • Save moos/34645e0468578c416469 to your computer and use it in GitHub Desktop.
Save moos/34645e0468578c416469 to your computer and use it in GitHub Desktop.
Backbone.Model.prototype.reset(attrs, options)
/**
* add reset() to Backbone.Model. Note 'id' attribute is kept.
*
* Unlike Backbone.Collection's native reset, no 'reset' event is fired.
*
* @param attrs {object} - hash of any new attributes to set, otherwise set to defaults
* @param options {object} - options to pass to set()
*/
Backbone.Model.prototype.reset = function(attrs, options) {
var opts = _.defaults(options || {}, {silent: true}),
idkey = this.idAttribute,
allAttrs = _.defaults(attrs || {}, this.defaults);
// keep old id if none was specified
if (!(idkey in allAttrs) && idkey in this.attributes) allAttrs[idkey] = this.attributes[idkey];
this.clear(opts).set(allAttrs, opts);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment