I am trying to wrap my head around how to render multiple ItemViews, each with a distinct model and template, in a CollectionView. The Marionette.js docs specify that a CollectionView supports a single ItemView. Am I wrong in making this assumption or can the CollectionView support this? If not, what is recommended? Thank you in advance for your assistance.
UPDATE:
I added the following code:
privObj.propertiesSubPanelView = new Marionette.CollectionView({
el: options.el,
collection: col,
getItemView: function( item ) {
console.log( item );
}
});
privObj.propertiesSubPanelView.render.done( function() {
console.log( 'Im done' );
});
which is giving the following error:
An `itemView` must be specified
UPDATE #2:
I have implemented the getItemView function within CollectionView as follows:
var ColView = Marionette.CollectionView.extend({
collection: col,
itemViews: views,
getItemView: function( item ) {
var viewId,
itemViewObj,
itemView;
viewId = item.get( 'name' );
itemViewObj = Marionette.getOption( this, 'itemViews' );
itemView = itemViewObj[viewId];
if ( _.isUndefined( itemView ) ) {
throw new Error( 'No view associated with name: ' + viewId );
}
return itemView;
}
});
var colView = new ColView();
var propLayout = new PropLayout();
propLayout.properties.show( colView );
However, I not get the following error (Uncaught TypeError: object is not a function) in:
Marionette.CollectionView.buildItemView: function(item, ItemViewType, itemViewOptions){
var options = _.extend({model: item}, itemViewOptions);
return new ItemViewType(options); <<== this line!
}
Did I miss something or is this a bug?
UPDATE #3
Here is my principle function...
newPropertiesSubPanelCollection: function( col, views ) {
var labelModel1 = new Backbone.Model({
name: 'Properties',
value: 'Properties',
data: undefined
});
var labelView1 = new Label_.Item();
var labelModel2 = new Backbone.Model({
name: 'Configure',
value: 'Properties',
data: undefined
});
var labelView2 = new Label_.Item();
var col = new Backbone.Collection();
col.add( labelModel1 );
col.add( labelModel2 );
var views = {};
views['Properties'] = labelView1;
views['Configure'] = labelView2;
var ColView = Marionette.CollectionView.extend({
collection: col,
itemViews: views,
getItemView: function( item ) {
var viewId,
itemViewObj,
itemView;
viewId = item.get( 'name' );
itemViewObj = Marionette.getOption( this, 'itemViews' );
itemView = itemViewObj[viewId];
if ( _.isUndefined( itemView ) ) {
throw new Error( 'No view associated with name: ' + viewId );
}
return itemView;
}
});
var colView = new ColView();
return this.propertiesSubPanelCollection = colView;
},