在模型上生成自定义主干错误.保存成功
Generate custom Backbone error on model.save success
我正在尝试覆盖我的模型的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);
}
});
感谢您的回复,尽管更改参数顺序对没有帮助
相关文章:
- 当与Dojo验证一起使用时,Chrome 41密码保存会做出错误的选择
- 使用odata 4的jaydata 1.5和保存/更新数据时的错误
- 在模型上生成自定义主干错误.保存成功
- Ajax文本区域保存错误
- Python在尝试保存新的JSON文件时导致服务器错误
- 缺少用于创建的模板.内部服务器错误-聚合物表单保存到数据库
- Chrome扩展表单保存错误
- "无法保存此网页“;Internet Explorer 8中出现错误
- 使用mongoose保存多个文档时的错误处理
- 尝试使用mongoose和async保存到DB时出现多个错误
- 保存通过Ajax返回的信息后出现$_POST错误
- mongoose findOne()调用未保存,输出中没有错误
- mongose保存失败,没有错误
- 保存模型返回错误
- 将html保存为xls-JS保存对话框错误
- imacros JavaScript 将数据保存到文件中,在 Firefox 中给出错误
- MongoDB 在保存数据时抛出错误(拓扑已损坏)
- 架构错误,“没有方法保存”
- SVG将画布IE安全错误保存到DataURL
- 更新/把错误保存在快递和猫鼬