Skip to content

Instantly share code, notes, and snippets.

@lbadura
Created January 22, 2014 10:12
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 lbadura/8556338 to your computer and use it in GitHub Desktop.
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
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