如何触发 model.save() 的成功回调?
this.model.save({
success: function(model, response){
console.log('success');
},
error: function(){
console.log('error');
}
})
模型已正确发布到处理保存的服务器,但未触发成功回调。我需要从服务器发回一些东西吗?
this.model.save({
success: function(model, response){
console.log('success');
},
error: function(){
console.log('error');
}
})
The model is correctly posted to the server which handles the save, but the success callback is not fired. Do I need to send something back from the server ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
save 的第一个参数是要保存在模型上的属性:
The first argument of save is the attributes to save on the model:
由于某些未知的原因,上述方法都不适合我。在我的例子中,只有 api 没有被命中。
但后来在搜索这个问题时,我遇到了 这个链接,其中有人尝试过
null
而不是{}
作为第一个参数。所以,这对我有用。希望这也对您有帮助。
For some unknown reason, none of the above method worked for me. The api only was not hit in my case.
But later while searching on this, I bumped into this link, where some one had tried
null
instead of{}
as the first parameter.so, this worked for me. Hope this helps you too.
您的服务器必须返回一个 JSON 对象。如果响应不是 JSON 对象,则不会触发回调。
如果要成功,您的服务器不返回 JSON 对象,请使用 dataType:"text" 选项执行保存,如下所示:
使用此选项,它不会等待 JSON 响应,而是等待文本,因此将启动回调。
Your server must return a JSON object. If the response is not a JSON object, the callbacks will not fire.
If for success your server doesn't return a JSON object, perform a save with dataType:"text" option, like this:
With this option it will not be waiting for a JSON in response, but a text, and thus the callback will be launched.
您可以使用下划线库,如下所示,因为主干已经依赖于此。请记住,save 的第一个参数必须具有属性,或者如果您想保存模型本身,您可以只传递 {}。
You may use underscore lib as follows as backbone already depends upon this. Remember first argument of save must either have attributes or you may just pass {} in case you want to save model itself.
所以我有点困惑 - 我是否仍然需要传递所有属性才能调用保存事件?如果我的模型很大怎么办..我不想手动设置每个属性
,我调用 model.save 并尝试执行以下操作:
好的,只是为了回答我自己的问题,以防有人发现这篇文章,我做了以下有效的操作:
编辑:由于某种原因我无法回复你,但我可以编辑
,但你不必设置 id:
this.model.get('id')
你可以只传递一个空白对象,因为空白属性不会扩展属性,也不执行任何操作:so im a little confused - do i still need to pass in all attributes in order for me to call a save event? what if my model is large.. i dont wish to set every property manually
im calling model.save and attempting to do the following:
ok just to answer my own question incase anyone finds this post, i did the following which works:
EDIT: I couldn't reply to you for some reason, but I can edit
but you don't have to set id:
this.model.get('id')
you can just pass a blank object because a blank attribute just won't extend attributes, does nothing:以下是我用于骨干模型保存的代码。
欢呼
罗伊·MJ
The following is the code that i am using for backbone model save.
Cheers
Roy M J
对于那些想要保存模型而不更新属性的人,您可以执行以下操作:
For those that want to save a model, without updating the attributes, you can do the following:
在初始化函数中,将同步方法绑定到您定义的方法(onSaveSuccess),
这样,每当您运行 this.model.save() 时,如果同步成功,它将运行 onSaveSuccess 函数作为回调
In you initialize function, bind the sync method to a method you define (onSaveSuccess)
This way, any time you run this.model.save(), it will run the onSaveSuccess function as a callback if your sync is successful