Reputation: 6403
Should the View hold my Collection (of models), or should the Model?
What is the standard approach? This is a generic question, I don't have a specific application example, but might the answer be that it depends on the situation?
Our small amount of work so far has Models holding Collections. Just reading about Marionette and its View, the documentation seems to suggest that Marionette makes the assumption that the View has the Collection.
Thoughts?
Upvotes: 1
Views: 224
Reputation: 486
A model shouldn't hold a collection unless the collection is a property of or involved in a relationship with that model. I assume in your question, when you say collection, you actually mean a collection of the models you also talk about.
The approach I'd recommend is to define a view that renders an individual model, and then an enclosing view which stores the collection and renders the individual view for each model in that collection.
Edit: For clarity, when I say enclosing, I mean on the DOM level, but not within your definitions.
This means that rendering the entire collection in one go is easy to do whenever you need, but you can also add or modify individual models in the collection and only have to render that model's individual view.
Upvotes: 3
Reputation: 6322
It would make sense to me that the view would hold a collection of models if appropriate. For instance, a histogramView may hold a collection of data points, which has its own model dataPoint. Then the histogramView can just have access to that collection and render everything from it in one go.
Of course, if the view need only own one model, that can be acceptable as well. In the case of a user profile view, you may want the view to own a userInformationModel.
Upvotes: 1