Answer the question
In order to leave comments, you need to log in
How to organize grouping of collections and models?
I am writing a web application using backbone.js and marionette.js.
There is a collection containing ~1000 models. Each model has a "crash" property.
There is a module that renders this collection in a CompositeView.
There is one more module that kicks the server to update the models and if something has changed, it executes model.set. After that, the model is re-rendered.
This system is now in operation.
The task is the following - you need to display the same models grouped according to the type of accident. That is, the name of the group should be displayed, followed by all models that have such an accident. If a model has several accidents, it is displayed in several groups. Within a group, models are sorted by some property.
Please help me implement such functionality - what data structures to use, how to render them, how to track crash changes and re-render the page? You can somehow try to solve the problem step by step - for starters, just render, forgetting for a while about updating the data.
Update (taken from the comment to the answer) : How to filter models is understandable, I'm more architecturally interested - will these filtered models become a separate collection?
Then it is necessary, apparently, in the module that monitors updates to emit an event that the model has changed, and the display module should already listen to these events and act accordingly. After all, it will not be possible to follow the change event on the models of the original collection if we copy them to the new collection?
Answer the question
In order to leave comments, you need to log in
You can apply the ViewModel approach.
create separate collections for each type of accident and a model for this collection, which will store a link to the (already real model). When initializing the page, you will need to create such "dummy" models for each crash. Thus, you will get a situation when the model itself will be in one collection (in your global collection of accidents, where ~ 1000 records), and "dummy" models containing real accident models will be displayed in the CompositeView.
The idea is to separate the storage logic from the display logic. Often, when using the backbone, you need to store data in the model that is only related to the display (for example, the status "selected" / "not selected").
Somehow it turned out chaotically, but I hope I managed to convey the idea.
UPD: With this approach, it is also convenient to track the appearance of new models in the main collection and, if necessary, add their desired groups
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question