Skip to content

Instantly share code, notes, and snippets.

@BrennanRoberts
Created January 12, 2012 05:52
Show Gist options
  • Save BrennanRoberts/1598992 to your computer and use it in GitHub Desktop.
Save BrennanRoberts/1598992 to your computer and use it in GitHub Desktop.
Smart Updating of Collections with Backbone
Backbone.Collection.prototype.update = function(col_in){
var that = this,
ids = [];
var cur_ids = that.pluck('id'),
new_ids = _(col_in).pluck('id'),
to_remove = _(cur_ids).difference(new_ids);
this.remove(to_remove);
_(col_in).each(function(mod_in){
if (that.get(mod_in.id)){
that.get(mod_in.id).set(mod_in);
} else {
that.add(mod_in);
}
});
this.trigger('update');
};
@infacq
Copy link

infacq commented Feb 23, 2014

what is the use ids = []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment