如何保存与Backbone.js的集合 [英] How to save a Collection with backbone.js

查看:105
本文介绍了如何保存与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.

层次结构重新psented作为一个集合$ P $和我用parentIds使用醇和李标签来构建树。

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上集合中的每个项目?没有任何一个有任何例如code?

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 /类别行动接受的哈希值{ID:parentId的} ,并更新了所有与此数据类别。

  • 保存时,我称之为 Backbone.sync('更新',categories_collection,{数据:a_hash_of_ {ID:PARENT_ID},成功:your_success_callback)。这应该工作,没有任何其他更重要的,如果指定网​​址为您的类别,并在成功回调你可以简单地重新采集新数据。

  • 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.

这是不充分的休息,但它发射只有一个更新请求的优势,不管你有多少种类有。

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天全站免登陆