简体   繁体   English

对ribs.js集合的项目进行分区(用于分组渲染)

[英]Partitioning items of a backbone.js collection (for grouped rendering)

Let's say, I have a backbone.js model named Item that is stored in an ItemCollection . 假设,我有一个名为Item的骨干ItemCollection模型,该模型存储在ItemCollection Given that Item has an attribute date , what would be the best approach to partition ItemCollection so that all items sharing the same date end up in the same partition? 给定Item具有一个属性date ,对ItemCollection进行分区的最佳方法是什么,以使共享相同日期的所有项目都位于同一分区中?

Ultimately, the partitioned data structure is supposed back a (backbone.js) view that is supposed to render the items grouped by date. 最终,应该将分区的数据结构返回到一个(backbone.js)视图,该视图应该呈现按日期分组的项目。

What would be the best approach for this? 最好的方法是什么?

So far I considered 到目前为止,我认为

  1. dynamically extracting the partitions when the index view is rendered, but that would probably break the elegance of backbone's event-based mechanism to render collection / model updates. 在呈现索引视图时动态提取分区,但这可能会破坏主干基于事件的机制来呈现集合/模型更新的优雅。

  2. representing each grouping header as an instance of a Header model and attaching a collection with its partition to it. 将每个分组标题表示为Header模型的实例,并将集合及其分区附加到该模型。 I would somehow need to keep the partitions attached to the Header instances in sync with the main ItemCollection , but that's probably manageable through backbone's eventing mechanism. 我将需要以某种方式使附加到Header实例的分区与主ItemCollection保持同步,但这可能可以通过主干的事件处理机制进行管理。

Would any of these approaches make sense? 这些方法有道理吗? Any other ideas? 还有其他想法吗?

PS: The grouping structure does not exist in my backend data model (and I would like to avoid introducing it), so this is some mapping that needs to happen on the fly on the client side PS:分组结构在我的后端数据模型中不存在(我想避免引入它),因此这是一些需要在客户端动态进行的映射

It seems like a hybrid of your two options would be workable. 您似乎可以同时使用这两个选项。 I would probably approach this as follows: 我可能会这样处理:

  1. Instead of Header , you can have an ItemGroup model (a little more descriptive) that renders the Item s inside of its collection , which would be a partition of the larger ItemCollection . 除了Header ,您还可以使用ItemGroup模型(更具描述性),将Item呈现在其collection的内部,该collection将是较大ItemCollection的分区。
  2. The partitioning itself can be done when you render the parent view, which I'll call AppView . 当您渲染父视图(我称为AppView时,可以完成分区本身。 In AppView#render , partition the data as you want. AppView#render ,根据需要对数据进行分区。 (You didn't explicitly ask how to partition the data, but you can use Underscore to do that in a few lines of code.) (您没有明确询问如何对数据进行分区,但是可以使用Underscore在几行代码中完成此操作。)
  3. Bind AppView#render to the add and remove event of ItemCollection , so that whenever a new item is added, the partitions re-render themselves. AppView#render绑定到ItemCollectionaddremove事件,以便无论何时添加新项目,分区都会自行重新渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM