So I have a compositeView. CompositeView is an "ul" element with base first "li" element and a collection of another "li" elements. My objective is to insert each rendered child collection of elements after base "li" element.
My views code
var
_NavItem = Marionette.ItemView.extend({
tagName: 'li',
template: function (serialized_model) {
return _.template('<a href="/"><span class="<%= name %>"><%= name %></span></a>')(serialized_model);
}
});
var
_NavComposite = Marionette.CompositeView.extend({
tagName: 'li',
className: 'header',
childView: _NavItem,
initialize: function () {
this.collection = new Backbone.Collection(this.model.attributes.items);
},
template: function (serialized_model) {
return _.template('<%= name %>')(serialized_model);
},
attachHtml: function(collectionView, childView){
collectionView.$el.append(childView.el);
}
});
var
_NavigationView = Marionette.CollectionView.extend({
tagName: 'ul',
childView: _NavComposite
});
and in fact this code will render structure
ul
li (base0) /li
li /li (li of collection)
li /li (li of collection)
...
li (base1) /li
li /li (li of collection)
li /li (li of collection)
...
li (base2) /li
li /li (li of collection)
li /li (li of collection)
...
/ul
but when I start this nothing happend. If I change code to
attachHtml: function(compositeView, childView){
compositeView.$el.append(childView.el);
}
it works fine, but this render another structure, that I want
ul
li (base0)
li /li (li of collection)
li /li (li of collection)
...
li /li (li of collection)
/li
li (base1)
li /li (li of collection)
li /li (li of collection)
...
li /li (li of collection)
/li
...
/ul
Structure of data
[0: { id: 1, name: 'Base li name 1', items: [
0: {id: 2, name: 'Li child 1'},
1: {id: 3, name: 'Li child 2'}
]},
1: { id: 4, name: 'Base li name 2', items: [
0: {id: 5, name: 'Li child 3'},
1: {id: 6, name: 'Li child 4'}
]}
...
]