我正在使用Backbone.js(版本0.5.3),并且在saving a model时遇到了一些成功回调的问题。即使模型已成功保存在服务器上,它仍未运行。为什么我的Backbone.js错误回调被调用,即使Rails应该返回成功响应?
的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的建议,我添加了一个错误回调,它正在运行。那么我应该从Ruby on Rails操作中返回什么,以便Backbone考虑保存成功?目前,我有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请求:
3210
如果你添加一个'error'回调函数呢?它会被叫吗? –
感谢您的建议,我编辑了这个问题。 – ben
我编辑了问题标题。你可能想重写它比这更好:/ – hugomg