I am trying to override Backbone.sync method for my model.
The server returns status : 200 OK and model fires success callback in all cases.
The server response is as follows
status: 200 OK
response : { statusCode: '0'; // 0 success, 1,2,3 errors ... }
I have overided the sync function to throw error callback the cases work fine and throw error callback
Although i am not able to pass any custom options to the error callback.
sync : function(method, model, options) {
var self = this, config = {};
var newError = function(method, success, error) {
return function(response, textStatus, jqXHR) {
if (response.statusCode === '0' && _.isFunction(success)) {
success(response, textStatus, jqXHR);
} else if (_.isFunction(error)) {
switch (response.statusCode) {
case '1':
//response.statusCode: '2'
//response.statusMessage: 'Servers error 1!”
textStatus = 'Servers error 1!';
break;
case '2':
//response.result: 'Server error 2'
//response.statusCode: '2'
textStatus = 'Servers error 1';
break;
}
error(response, jqXHR, textStatus ); //arguments fail maybe backbone overrides them before firing my error callback
}
};
};
config.success = newError(method, options.success, options.error);
// add API call configuration to the `options` object
options = _.extend(options, config);
return Backbone.Model.prototype.sync.call(this, method, model, options);
}
// the following error callback is invoked but the message is not passed to it
model.save(data,{
success : successCB,
error : function(args){
alert('error'); //works
//args.textStatus or something similar passed from above sync logic
}
})
I am aware that something is wrong at this line. error(response, jqXHR, textStatus );
Please let me know how to pass the textStatus or other options the error callback