Created
July 29, 2013 20:16
-
-
Save arunkjn/6107407 to your computer and use it in GitHub Desktop.
Backbone global sync error handler
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
$(document).ajaxError(function(event, request, settings) { | |
//HANDLE GLOBAL AJAX ERRORS HERE | |
}); | |
Backbone.originalSync = Backbone.sync; | |
Backbone.sync = function (method, model, options) { | |
var errorOverride = options.errorOverride; | |
if(errorOverride){ | |
options.global = typeof errorOverride !== "function"; | |
options.errorOverride = function(resp){ | |
errorOverride(model, resp, options); | |
model.trigger('error', model, resp, options); | |
} | |
} | |
return Backbone.originalSync(method, model, _.extend(options, {error: options.errorOverride || options.error})); | |
}; | |
var M = Backbone.Model.extend({ | |
url: '/some/non-existent/path' | |
}); | |
var m = new M(); | |
m.save(null, { | |
error: function() { | |
console.log("options error", arguments); | |
}, | |
errorOverride: function() { | |
console.log("options error arun", arguments); | |
}, | |
success: function() { | |
console.log("options success", arguments); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment