Skip to content

Instantly share code, notes, and snippets.

@hc2p
Created August 6, 2014 13:03
Show Gist options
  • Save hc2p/bf93436bcb5ac58b88d6 to your computer and use it in GitHub Desktop.
Save hc2p/bf93436bcb5ac58b88d6 to your computer and use it in GitHub Desktop.
extended Collection.prototype.fetch, added before:add:all and add:all event before and after 'add' events
orgFetch = Backbone.Collection::fetch
Backbone.Collection::fetch = (options={}) ->
success = options.success
options.success = (collection, resp, options) ->
collection.trigger 'before:add:all', collection, resp, options
collection.each (model) ->
model.trigger('add', model, collection, options)
collection.trigger 'add:all', collection, resp, options
success(collection, resp, options) if success
options.silent = true
orgFetch.call(this, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment