Reputation: 111072
I'm locking for a way, where all my collections and models use the same logic to display errors when fetch or save fail. But I dont wanna wrote the onError callback all over again. The goal is to have on methode that open error dialogs depending on the http error code of the response when it fails.
Upvotes: 0
Views: 263
Reputation: 12467
Another way to do it would be to create a base model and collection and have your models and collections extend those instead of Backbone's:
var BaseModel = Backbone.Model.extend({
onSyncError: function(model, response) {
// your error-handling code
},
onSyncSuccess: function(model, response) {
// do stuff if successful
},
// Backbone will call your 'sync' if it exists
sync: function(method, model, options) {
options.error = this.onSyncError;
options.success = this.onSyncSuccess;
Backbone.sync.call(this, method, model, options);
}
});
And then in your model:
var MyModel = BaseModel.extend({
// model stuff
});
Upvotes: 2
Reputation: 111072
Thinking about it, I came up with this solution:
function callback(success){
this.success = sucess;
}
callback.prototyp.error = function(model, response){
// central error handling here
}
myModel.save(new callback(myModel.success))
Upvotes: 1