Created
July 16, 2014 13:54
-
-
Save sdyxch/0a3ecf28832667fdd1b3 to your computer and use it in GitHub Desktop.
backbone sync
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
_.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