Skip to content

Instantly share code, notes, and snippets.

@yuchi
Created October 20, 2011 12:23
Show Gist options
  • Save yuchi/1301017 to your computer and use it in GitHub Desktop.
Save yuchi/1301017 to your computer and use it in GitHub Desktop.
Uniqueness by id in Backbone.Collection
var _oldPrepareModel = Backbone.Collection.prototype._prepareModel;
Backbone.Collection.prototype._prepareModel = function (model, options) {
var _id = model.id;
if (!(model instanceof Backbone.Model)) _id = model[this.model.prototype.idAttribute];
if (this._byId[_id]) return this.get(_id);
return _oldPrepareModel.call(this, model, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment