Skip to content

Instantly share code, notes, and snippets.

@insin
Created September 4, 2012 10:47
Show Gist options
  • Save insin/3619992 to your computer and use it in GitHub Desktop.
Save insin/3619992 to your computer and use it in GitHub Desktop.
Backbone.Collection.prototype.move
/**
* Moves a model to the given index, if different from its current index. Handy
* for shuffling models about after they've been pulled into a new position via
* drag and drop.
*/
Backbone.Collection.prototype.move = function(model, toIndex) {
var fromIndex = this.indexOf(model)
if (fromIndex == -1) {
throw new Error("Can't move a model that's not in the collection")
}
if (fromIndex !== toIndex) {
this.models.splice(toIndex, 0, this.models.splice(fromIndex, 1)[0])
}
}
@bendulum
Copy link

How could this be amended so a model will be inserted automatically by an attribute of the model going through the comparator of the collection? Thanks!

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