Created
March 29, 2012 15:53
-
-
Save bluegraybox/2238864 to your computer and use it in GitHub Desktop.
Extending Backbone collections
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
@LIST = (path="", model=null, opts={}) -> | |
opts.model = model if model | |
Backbone.Collection.extend $.extend opts, | |
parse: ({data}) -> data | |
comparator: (item) -> | |
compare_item(item) | |
url: API path | |
@REVERSE_LIST = (path="", model=null, opts={}) -> | |
list = @LIST(path, model, opts) | |
$.extend list, | |
comparator: (item)-> | |
value = -compare_item(item) | |
value | |
list |
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
@LIST = (path="", model=null, opts={}) -> | |
opts.model = model if model | |
Backbone.Collection.extend $.extend opts, | |
parse: ({data}) -> data | |
comparator: (item) -> | |
compare_item(item) | |
url: API path | |
@REVERSE_LIST = (path="", model=null, opts={}) -> | |
list = @LIST(path, model, opts) | |
# extend does not modify list in place | |
list.extend comparator: (item)-> | |
value = -compare_item(item) | |
value | |
# This returns the original version of list, not the extended one | |
list |
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
@REVERSE_LIST = (path="", model=null, opts={}) -> | |
@LIST(path, model, opts).extend comparator: (item)-> | |
-compare_item(item) |
audreyt
commented
Mar 29, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment