I have a collection rendering well using Backbone/Marionette/Mustache.
So PersonModel
and PersonView
are working fine. PersonView
has a mustache template called PersonTemplate
that is being loaded perfectly by my PeopleCollection
and PeopleCollectionView
and rendering inside a default tagname: "div"
.
However, I want to render this collection inside of another template so I can put some headers there.
Right now it's essentially:
<div> <!-- collection tagName -->
<div class="person"> <!-- PersonTemplate -->
<div class="person-name">John</div>
</div>
<div class ="person"> <!-- PersonTemplate -->
<div class="person-name">Dave</div>
</div>
</div>
But I would like it to be:
<div id="people> <!-- CollectionTemplate -->
<h1>People</h1> <!-- CollectionTemplate -->
<div class="person"> <!-- PersonTemplate -->
<div class="person-name">John</div>
</div>
<div class ="person"> <!-- PersonTemplate -->
<div class="person-name">Dave</div>
</div>
</div>
I understand I can add these items with jQuery after the fact, but I was hoping to avoid that because my actual CollectionTemplate
is much more complex than the example given here.
CompositeView is exactly what you need. Here is how I have CompositeView setup for simple notes panel.
Item View for each note:
The Composite View:
Now the templates:
panelTemplate:
And the row template:
In your CompositeView definition you specifiy what template you want to use (panelTemplate) in my example. Than you specify which ItemView to use for each model in the collection that composite view holds. (View.NoteRow in my example). Than you define the container within your template that each ItemView will go into ( I'm each of my ItemViews into )