Created
January 22, 2014 10:12
-
-
Save lbadura/8556338 to your computer and use it in GitHub Desktop.
Render an authorization error if any of Backbone's syncs result with 401 response
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
originalSync = Backbone.sync | |
app = this | |
Backbone.sync = (method, model, opts) -> | |
dfd = $.Deferred() | |
if opts | |
dfd.then(opts.success, opts.error) | |
xhr = originalSync(method, model, _.omit(opts, 'success', 'error')) | |
xhr.done(dfd.resolve) | |
xhr.fail -> | |
if (xhr.status == 401) | |
app.auth.error() | |
dfd.reject.apply(xhr, arguments) | |
return dfd.promise() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment