Skip to content

Instantly share code, notes, and snippets.

@arunkjn
Created July 29, 2013 20:16
Show Gist options
  • Save arunkjn/6107407 to your computer and use it in GitHub Desktop.
Save arunkjn/6107407 to your computer and use it in GitHub Desktop.
Backbone global sync error handler
$(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