I'm getting into larger-scale data structures with Backbone, and coming across occasions where data would be well-represented via CompositeViews; that is, CollectionViews with the addition of "added fluff" around them, such as headers, buttons, and so on.
However, I'm having a lot of difficulty nesting CompositeViews inside one another. Using the standard itemView property on a CompositeView to render another CompositeView doesn't seem to be firing at all.
Assume I have a parent ItemView, instantiated as such (following Derick Bailey's example; assume this top level is where the initial fetch()
on the collection would be called):
var User = Backbone.Model.extend({});
var UserCollection = Backbone.Collection.extend({
model: User
});
var userList = new UserCollection(userData);
var schedulerCompositeView = new SchedulerCompositeView({
collection: userList
});
schedulerCompositeView.render();
this.ui.schedulerWidget.html(schedulerCompositeView.el);
And SchedulerCompositeView looks as such:
return Backbone.Marionette.CompositeView.extend({
template: Handlebars.compile(schedulerCompositeViewTemplate),
itemView: SchedulerDetailCompositeView,
appendHtml: function (collectionView, itemView) {
collectionView.$("#schedulerGroups").append(itemView.el);
}
});
And finally, SchedulerDetailCompositeView:
return Backbone.Marionette.CompositeView.extend({
template: Handlebars.compile(schedulerDetailCompositeViewTemplate),
itemView: SchedulerDetailItemView,
appendHtml: function (collectionView, itemView) {
collectionView.$("#schedulerDetailStops").append(itemView.el);
}
});
SchedulerDetailCompositeView never gets instantiated; in fact, its appendHtml()
method never seems to fire at all.
Clearly there's some other information missing here; obviously the intent is to pass a collection/model to SchedulerDetailCompositeView, but I'm not really sure what the proper technique for doing so is, due to the nested CompositeViews.
For reference, here's a rough mockup of the structure I'm trying to achieve; if there might be a better manner for reaching this goal than nested CompositeViews, I'm certainly all ears:
http://jsfiddle.net/KAWB8/