[英]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 到目前为止,我认为
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. 在呈现索引视图时动态提取分区,但这可能会破坏主干基于事件的机制来呈现集合/模型更新的优雅。
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: 我可能会这样处理:
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
的分区。 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在几行代码中完成此操作。) 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
绑定到ItemCollection
的add
和remove
事件,以便无论何时添加新项目,分区都会自行重新渲染。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.