【发布时间】:2011-10-06 01:24:21
【问题描述】:
我正在使用 Backbone.js(版本 0.5.3)并且在 saving a model 时遇到了一些成功回调问题。即使模型已成功保存在服务器上,它也没有运行。
CoffeeScript:
console.log 'in switch_private'
console.log "private_entry attribute is currently #{@model.get('private_entry')}"
@model.save {'private_entry': true},
success: ->
console.log 'in success'
编译的Javascript:
console.log('in switch_private');
console.log("private_entry attribute is currently " + (this.model.get('private_entry')));
return this.model.save({
'private_entry': true
}, {
success: function() {
return console.log('in success');
}
});
控制台输出:
in switch_private
private_entry attribute is currently false
XHR finished loading: "http://localhost:3000/entries/235".
我从 Ruby on Rails 中的更新操作返回 head :ok。
添加模型和响应参数,使其成为success: (model, response) ->,并没有什么不同。出了什么问题?
编辑:
根据 Trevor Burnham 的建议,我添加了一个错误回调,并且它正在运行。那么,为了让 Backbone 认为保存成功,我应该从 Ruby on Rails 操作返回什么?目前我有head :ok
编辑 2:这是我更新后的编译 Javascript:
var this_view;
this_view = this;
return this.model.save({
'private_entry': !(this.model.get('private_entry'))
}, {
success: function(model, response) {
return console.log('in success');
},
error: function(model, response) {
return console.log('in error');
}
});
这是 PUT 请求:
【问题讨论】:
-
如果你也添加一个
error回调呢?它会被调用吗? -
感谢您的建议,我已经编辑了问题。
-
我编辑了问题标题。不过,您可能希望更好地重写它:/
标签: javascript backbone.js coffeescript