Created
March 9, 2011 04:14
-
-
Save iamjwc/861684 to your computer and use it in GitHub Desktop.
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
Move = | |
move: (modelOrIndex, endIndex) -> | |
if typeof modelOrIndex == "number" | |
this.__moveIndexTo__(modelOrIndex, endIndex) | |
else | |
this.__moveModelTo__(modelOrIndex, endIndex) | |
__moveModelTo__: (model, endIndex) -> | |
this.__moveIndexTo__ _.indexOf(this.models, model), endIndex | |
__moveIndexTo__: (startIndex, endIndex) -> | |
movedUp = startIndex > endIndex | |
movedDown = startIndex < endIndex | |
_.each this.models, (model, i) -> | |
model._index = if i == startIndex | |
endIndex | |
else if movedDown && i <= endIndex | |
i - 1 | |
else if movedUp && i >= endIndex | |
i + 1 | |
else | |
i | |
# Save old comparator | |
c = this.comparator | |
# Build comparator on _index just set | |
# and sort on this index. | |
this.comparator = (model) -> model._index | |
this.sort() | |
# Return comparator to old value | |
this.comparator = c | |
_.extend Backbone.Collection.prototype, Move |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment