BackboneJS,创建和销毁,但没有更新方法?
我注意到,在 BacbkoneJS 中,Collection 对象中有一个方法,允许您向该集合添加新模型,同时将新模型推送到服务器。这很方便,因为如果 ajax 请求成功,它只会将新模型添加到集合中。它还会使用从服务器返回的 ID 更新模型上的 ID。
Model 对象上还有一个类似的方法,称为 destroy,如果 ajax 请求返回 200,这只会销毁模型。
我如何通过 Update 实现类似的功能,当我在模型上设置数据时,它会尝试将模型保存到服务器,确保 200 状态,然后触发“更改”事件?
问题是如果我设置模型数据(我必须这样做才能调用 save()),则会触发更改事件。
谢谢
I noticed that in BacbkoneJS there is a method in the Collection object that allows you to add a new model to that collection, while at the same time pushing the new model to the Server. This is convenient because it will only add the new model to the collection if the ajax request is successful. It will also update the ID on the model with the one returned from the server.
There is also a similar method on the Model object called destroy, this will only destroy the model if the ajax request returns 200.
How can I achieve something similar to this with Update, where when I set data on my model, it will attempt to save the model to the server, ensure a 200 status, and then fire the "change" event?
The problem is If I set the model data (I have to do this in order to call save()), then the change event fires.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Backbone.Model.save() 方法允许您指定要更改的属性。查看带注释的源代码,它看起来像“模型.set()”发生在服务器成功响应时。
因此,不应执行
Do
,而是应在成功保存后触发更改事件。
注意:我还没有对此进行测试 - 这只是阅读源代码。
The Backbone.Model.save() method allows you to specify the properties you want to change. Looking at the annotated source code, it looks like the "model.set()" happens on a successful response from the server.
So, instead of doing
Do
and the change event should fire after a successful save.
Note: I have not tested this - this is just from reading the source.