我正在尝试保存模型并在成功时取消渲染它:问题是在成功中我无法引用 this 引用(即视图),我也无法引用 this.model 的变量 isOk.status。 save(...) 返回。编码:
save: function(e) {
e.preventDefault();
var isOk = this.model.save(null,
{
wait: true,
success: function(model, response){
console.log(response);
console.log(response.status);
},
error: function(model, response){
console.log("error");
console.log($.parseJSON(response.responseText));
$('#errorMessage').empty();
$('#errorMessage').append($.parseJSON(response.responseText).error);
$('#errorApproveModal').modal({
keyboard: true
});
}
});
console.log('logging isOk');
console.log(isOk);
//this one is working! It's on validate event
if(!isOk){
$('#errorMessage').empty();
$('#errorMessage').append("Error: there was an error");
$('#errorApproveModal').modal({
keyboard: true
});
return false
}
console.log(isOk);
**//both those checks are not working for some reason.**
//
if(isOk.status == 200 || isOk.statusText == "OK"){
console.log('in is ok');
this.remove();
}
return false;
}
顺便说一句,观点是:
App.Views.User = Backbone.View.extend({
model: App.Models.User
,
save: function...
});
有人可以帮忙吗?有没有比这种方法更好的方法来处理成功和错误?谢谢!!罗伊