Skip to content

Instantly share code, notes, and snippets.

@arunkjn
Last active December 20, 2015 09:19
Show Gist options
  • Save arunkjn/6107114 to your computer and use it in GitHub Desktop.
Save arunkjn/6107114 to your computer and use it in GitHub Desktop.
Backbone Model global error hadling
Backbone.originalSync = Backbone.sync;
Backbone.sync = function (method, model, options) {
var errorOverride = options.errorOverride;
if(errorOverride){
options.errorOverride = function(resp){
errorOverride(model, resp, options);
model.trigger('error', model, resp, options);
}
}
var xhr;
xhr = Backbone.originalSync(method, model, _.extend(options, {error: options.errorOverride || options.error}));
xhr.fail(function(){
if(!options.errorOverride){
//HANDLE GLOBAL ERRORS HERE
console.log("Global handler called");
}
});
return xhr;
};
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