Skip to content

Instantly share code, notes, and snippets.

@bluegraybox
Created March 29, 2012 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluegraybox/2238864 to your computer and use it in GitHub Desktop.
Save bluegraybox/2238864 to your computer and use it in GitHub Desktop.
Extending Backbone collections
@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
@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
@REVERSE_LIST = (path="", model=null, opts={}) ->
@LIST(path, model, opts).extend comparator: (item)->
-compare_item(item)
@audreyt
Copy link

audreyt commented Mar 29, 2012

@REVERSE_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 

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