-
-
Save markandrewj/4239d56f39912a33c450 to your computer and use it in GitHub Desktop.
Smart Updating of Collections with Backbone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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