在模型上生成自定义主干错误.保存成功

Generate custom Backbone error on model.save success

本文关键字:错误 保存 成功 自定义 模型      更新时间:2023-09-26

我正在尝试覆盖我的模型的Backbone.sync方法。

服务器返回状态:200 OK,模型在所有情况下都会触发成功回调。

服务器响应如下

状态:200 OK

响应:{statusCode:"0";//0成功,1,2,3错误。。。}

我已经过多地使用了同步函数来抛出错误回调用例工作正常并抛出错误回调

尽管我无法将任何自定义选项传递给错误回调。

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);
}

//调用了以下错误回调,但消息没有传递给它

     model.save(data,{
       success : successCB,
       error : function(args){
            alert('error'); //works
           //args.textStatus or something similar passed from above sync logic
        }
     })

我知道这条线路出了问题。错误(响应,jqXHR,textStatus);

请让我知道如何将textStatus或其他选项传递给错误回调

错误函数中的参数顺序似乎有问题。签名应为(jqXHR jqXHR, String textStatus, String errorThrown)

以下要点有助于common.js

根据以上要点,行号106至128通过将以下行添加到来解决问题

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

感谢您的回复,尽管更改参数顺序对没有帮助