1

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

user2179539
  • 116
  • 1
  • 8

2 Answers2

0

Seems to be an issue with the order of parameters in your error function. The signature should be (jqXHR jqXHR, String textStatus, String errorThrown).

squall3d
  • 1,737
  • 14
  • 12
0

The following gist helped common.js

From the above gist line numbers 106 to 128 solved the problem by adding the following lines to it

        response.done(function(){
                if(response.responseJSON.statusCode &&
                        response.responseJSON.statusCode === '0'){
                    response.done(deferred.resolve);
                } else {
                    deferred.rejectWith(response, arguments);
                }
        });

Thanks for the reply, although changing the sequence of parameters did not help

user2179539
  • 116
  • 1
  • 8