Last active
December 20, 2015 09:19
-
-
Save arunkjn/6107114 to your computer and use it in GitHub Desktop.
Backbone Model global error hadling
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
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