Skip to content

Instantly share code, notes, and snippets.

@markandrewj
Forked from BrennanRoberts/gist:1598992
Last active August 29, 2015 14:11
Show Gist options
  • Save markandrewj/4239d56f39912a33c450 to your computer and use it in GitHub Desktop.
Save markandrewj/4239d56f39912a33c450 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');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment