Skip to content

Instantly share code, notes, and snippets.

@sdyxch
Created July 16, 2014 13:54
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 sdyxch/0a3ecf28832667fdd1b3 to your computer and use it in GitHub Desktop.
Save sdyxch/0a3ecf28832667fdd1b3 to your computer and use it in GitHub Desktop.
backbone sync
_.each ['Model', 'Collection'], (name) ->
ctor = Backbone[name]
fetch = ctor::fetch
ctor::fetch = (options={}) ->
options = _.clone options
success = options.success
error = options.error
options.success = =>
@trigger 'fetched', @
sucess.apply null, arguments if success
options.error = =>
@trigger 'fetcherror', @
error.apply null, arguments if error
@trigger 'fetch', @
fetch.apply @, options
sync = Backbone.sync
Backbone.sync = (method, model, options={}) ->
switch method
when 'read'
options = _.clone options
success = options.success
error = options.error
options.success = =>
model.trigger 'fetched', model
sucess.apply null, arguments if success
options.error = =>
model.trigger 'fetcherror', model
error.apply null, arguments if error
model.trigger 'fetch', model
sync.apply this, arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment