Javascript 主干存储模型问题
我正在尝试保存一个模型,一旦成功,请取消该模型: 问题是,在success中,我不能引用this引用(即视图),也不能引用this.model.save(…)返回的变量isOk.status。 守则:Javascript 主干存储模型问题,javascript,backbone.js,Javascript,Backbone.js,我正在尝试保存一个模型,一旦成功,请取消该模型: 问题是,在success中,我不能引用this引用(即视图),也不能引用this.model.save(…)返回的变量isOk.status。 守则: save: function(e) { e.preventDefault(); var isOk = this.model.save(null, { wait: true, success: function(mo
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...
});
有人能帮忙吗?
有没有比这种方法更好的方法来处理成功和错误?
谢谢
Roy我不确定这样做是否正确,但我总是从视图的函数中声明一个引用此的变量,然后成功地使用它。大概是这样的:
save: function(e) {
// ADD THIS LINE
var me = this;
var isOk = this.model.save(null,
{
....
success: function(model, response){
// USE me IN HERE
me.render(); // e.g
},
....
}
您也可以这样做:
保存:功能(e){
使用这些选项,您可以执行所有参数
var isOk = this.model.save(null,
{
....
success: function(model, response,options){
// USE me IN HERE
this.options.me.render(); // e.g
},
//ADD me this
me : this
....
}