Let’s say, I have a backbone.js model named Item that is stored in an 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?
Ultimately, the partitioned data structure is supposed back a (backbone.js) view that is supposed to render the items grouped by date.
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
Headermodel and attaching a collection with its partition to it. I would somehow need to keep the partitions attached to theHeaderinstances in sync with the mainItemCollection, but that’s probably manageable through backbone’s eventing mechanism.
Would any of these approaches make sense? Any other ideas?
P.S.: 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
It seems like a hybrid of your two options would be workable. I would probably approach this as follows:
Header, you can have anItemGroupmodel (a little more descriptive) that renders theItems inside of itscollection, which would be a partition of the largerItemCollection.AppView. InAppView#render, partition the data as you want. (You didn’t explicitly ask how to partition the data, but you can use Underscore to do that in a few lines of code.)AppView#renderto theaddandremoveevent ofItemCollection, so that whenever a new item is added, the partitions re-render themselves.