简体   繁体   English

在model.save成功上生成自定义Backbone错误

[英]Generate custom Backbone error on model.save success

I am trying to override Backbone.sync method for my model. 我正在尝试为我的模型覆盖Backbone.sync方法。

The server returns status : 200 OK and model fires success callback in all cases. 服务器返回状态:200 OK,并且模型在所有情况下都会触发成功回调。

The server response is as follows 服务器响应如下

status: 200 OK 状态:200 OK

response : { statusCode: '0'; 响应:{statusCode:'0'; // 0 success, 1,2,3 errors ... } // 0成功,1,2,3错误...}

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 ); 错误(响应,jqXHR,textStatus);

Please let me know how to pass the textStatus or other options the error callback 请让我知道如何将textStatus或其他选项传递给错误回调

Seems to be an issue with the order of parameters in your error function. 似乎与错误函数中的参数顺序有关。 The signature should be (jqXHR jqXHR, String textStatus, String errorThrown) . 签名应为(jqXHR jqXHR, String textStatus, String errorThrown)

The following gist helped common.js 以下要点帮助common.js

From the above gist line numbers 106 to 128 solved the problem by adding the following lines to it 从上面的要点行号106到128通过添加以下行来解决该问题

        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 感谢您的答复,尽管更改参数顺序没有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM