保存整个集合的最佳实践?
假设我有一个集合,并且我对其许多模型进行了更改。使用单个 HTTP 请求保存所有更改的最佳方法是什么?
Say that I have a Collection and I've made changes to many of its Models. What's the best way to save all of the changes using a single HTTP request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,REST 后端处理单个实例创建/更新。您需要更改它以接受对象数组。
也就是说,在客户端,您需要直接转到 Backbone.sync 函数,
在这种情况下,您的模型应该是模型数组。该方法应该是“创建”或“保存”,并且选项采用与 jQuery ajax 调用相同类型的选项(错误、成功等)
Usually REST backends handle single instance creation/update. You would need to change that to accept an array of objects.
That said, on the client side, you would need to go directly to the Backbone.sync function
In this case your model should be an array of model. The method should be "create" or "save" and the options take the same type of options as a jQuery ajax call (error, success, etc.)
我将在这里做错误的事情并引用维基百科关于正确的 RESTful 实践: PUT 到
example.com/resources
应将整个集合替换为另一个集合。基于此,当我们需要支持同时编辑多个项目时,我们写了这个合约。{"resources": [{resource1},{resource2}]}
{"resources": [{"id":1,...},{"id":2,...}]}
我们在 Rails 中编写了合约的服务器部分,但这是客户端部分(在 CoffeeScript 中,抱歉!):
我认为这比覆盖 Backbone.sync 更容易实现。对代码的一项评论是,我们的集合始终是子对象,这应该解释为什么每当将对象添加到集合中时代码都会设置“parent_id”,以及 URL 的根如何是父级的 URL。如果您有要修改的根级集合,则只需删除
@parent
业务即可。I'm going to do the Wrong Thing here and quote Wikipedia regarding proper RESTful practices: a PUT to
example.com/resources
should replace the entire collection with another collection. Based on this, when we had to support editing multiple items simultaneously, we wrote up this contract.{"resources": [{resource1},{resource2}]}
{"resources": [{"id":1,...},{"id":2,...}]}
We wrote the server half of the contract in Rails, but here's the client half (in CoffeeScript, sorry!):
I thought this was a lot easier to implement then overriding Backbone.sync. One comment on the code, our collections were always child objects, which should explain why the code sets a "parent_id" whenever an object is added to the collection, and how the root of the URL is the parent's URL. If you have root-level collections that you want to modify, then just remove the
@parent
business.您应该扩展 Backbone.Collection,为其提供一个 save() 方法来检查每个模型的 hasChanged() 方法。
然后它应该调用 Backbone.sync,您可能需要将其扩展为自定义同步函数。如果您确实使用自定义
Backbone.sync
函数,请务必在您的集合上设置它。一种不同的方法(使用模型来表示集合)在这里:“如何”在 Backbone.js 中保存整个集合 - Backbone.sync 或 jQuery.ajax?
我也喜欢 https://stackoverflow.com/a/7986982/137067
You should extend
Backbone.Collection
, giving it asave()
method that would check each of its modelshasChanged()
.Then it should call
Backbone.sync
, which you'll probably have to extend a little into a custom sync function. If you do use a customBackbone.sync
function, then be sure to set it on your collection.A different approach (using a model to represent the collection) is here: "How" to save an entire collection in Backbone.js - Backbone.sync or jQuery.ajax?
I also like https://stackoverflow.com/a/7986982/137067
这段代码向集合原型添加了一个新方法,只是为了调用已更改的模型的 save 方法。它对我有用:
要点链接: https://gist.github.com/julianitor/701c677279bac1529b88
This code adds a new method to the collection prototype just to call the save method of those models that had changed. It worked for me:
Gist link: https://gist.github.com/julianitor/701c677279bac1529b88