This question is an extension of this Related question.
Taking Derick's advice, I now have my data in the correct shape. i.e. I have a collection of Department
objects, each of which have a collection of Users
.
Again following Derick's advice I'm trying to render a CollectionView
of CompositeView
's
My collection view looks like this
class UserListView extends Backbone.Marionette.CollectionView
itemView: UserCompositeView
id: "user-list"
appendHtml: (collectionView, itemView, index) =>
itemModel = @collection.at(index)
itemView = new UserCompositeView
model: itemModel
collection: itemModel.get("users")
collectionView.$el.append itemView.el
and my Composite View
looks like this:
class UserCompositeView extends Backbone.Marionette.CompositeView
itemView: UserItemView
itemViewContainer: '#users'
If I don't override the appendHtml method then the view renders but it only renders the properties of the Department
model. It doesn't render the users
collection.
When I override the appendHtml
method in the CollectionView
so I can pass a model (a Department
object) and a collection of users
but one or both of them seem to be the wrong type of objects because the Marionette bindTo
function is complaining that the object has no 'on' method.
What am I doing wrong?
What does
itemModel.get("users")
return?If this is returning a JavaScript array or object literal, that would be the problem. You have to pass a valid Backbone.Collection as the collection parameter, not just an array of objects.
collection: new Backbone.Collection(itemModel.get("users"))
You can also look into using backbone-relational which will let you define models that have submodels or subcollections.