Skip to content

Instantly share code, notes, and snippets.

@fearphage
Created October 4, 2011 15:27
Show Gist options
  • Save fearphage/1261938 to your computer and use it in GitHub Desktop.
Save fearphage/1261938 to your computer and use it in GitHub Desktop.
Backbone.Collection#update
Backbone.Collection.prototype.update = function(collection) {
var _ids = _(collection)
.chain()
.reduce(function(ids, model) {
var
id = model.id
,needle = this.get(id)
;
if (needle) {
needle.set(model);
}
else {
this.add(model);
}
return ids.concat(model.id);
}
,[]
,this
)
;
this.remove(this.reject(function(model) {
return _ids.include(model.id);
}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment