Created
October 4, 2011 15:27
-
-
Save fearphage/1261938 to your computer and use it in GitHub Desktop.
Backbone.Collection#update
This file contains 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(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