如何使用backbone.js保存集合 [英] How to save a Collection with backbone.js

查看:22
本文介绍了如何使用backbone.js保存集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类别层次结构.我使用 jquery 库作为层次结构,以按照用户想要的方式将所有内容打乱.然后他们点击保存.所以初始层级和要保存的层级可能完全不同.

I have a hierarchy of categories. I use a jquery library for the hierarchy to get all jumbled up the way the user wants. Then they click save. So the initial hierarchy and hierarchy to be saved could be totally different.

层次结构表示为一个集合,我使用 parentIds 使用 ol 和 li 标签构建一棵树.

The hierarchy is represented as a collection and I use parentIds to build a tree using ol and li tags.

当用户点击保存时,我需要用新的 parentId 更新集合中的所有项目,并将每个项目与服务器同步.

When the user clicks save, I then need to update all the items in the collection with their new parentId and sync each one with the server.

我想知道是否有人对如何进行这里有任何建议.我在 Backbone.sync 的文档中看到,使用 setTimeout 将快速更新批处理到单个请求中."所以,如果我理解正确,我会将每个调用排队到 Backbone.sync 然后在几秒钟后使用 setTimeout 将我的队列发送到服务器?

I'm wondering if anyone has any advice on how to proceed here. I've seen in the documentation for Backbone.sync, ''Use setTimeout to batch rapid-fire updates into a single request.'' So, if I understand correctly, I would queue each of the calls to Backbone.sync and then use setTimeout to send my queue to the server after a few seconds?

另外,如果我重写 Backbone.sync,我是否仍然需要一个保存"方法来解析响应的 json(服务器响应必须发回一个对象列表)和然后在集合中的每个项目上调用 model.set ?有人有任何示例代码吗?

Also, if I rewrite Backbone.sync, don't I still need a 'save' method somewhere for the collection that will parse the json of the response (the server response would have to send back a list of objects) and then call model.set on each item in the collection? Does any one have any example code?

谢谢!

推荐答案

我不确定我是否完全理解上下文.它是这样的:你有一个包含所有类别的集合,并使用属性 parentId 保持层次结构.用户更改了层次结构,然后您想要保存层次结构(隐式保存现在包含不同 parentId 的集合).

I'm not sure I fully understand the context. It is something like this: you have one collection with all the categories, and keep the hierarchy using the property parentId. The user changes the hierarchy and then you want to save the hierarchy (implicitly, save the collection that now contains different parentId's).

如果这是正确的,我会做的是:

If that is correct, what I would do is:

  • 进行 PUT/categories 操作,该操作接受 {id: parentId} 的哈希值,并使用此数据更新所有类别.
  • 保存时,我会调用Backbone.sync('update', Categories_collection, {data: a_hash_of_{id:parent_id}, success: your_success_callback).如果您为类别指定 url,并且在成功回调中,您可以简单地使用新数据重置集合,那么这应该可以在没有任何其他覆盖的情况下工作.
  • make a PUT /categories action that accepts a hash of {id: parentId} and updates the the all the categories with this data.
  • when saving, I would call Backbone.sync('update', categories_collection, {data: a_hash_of_{id:parent_id}, success: your_success_callback). This should work without any other overriding, if you specify url for your categories, and in the success callback you can simply reset the collection with the new data.

这不是完整的 REST,但它的优点是无论您有多少类别,都只触发一个更新请求.

This is not full REST, but it has the advantage of firing only one update request no matter how many categories you have.

这篇关于如何使用backbone.js保存集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆