Backbone.Marionette CollectionView/CompositeView呈现 [英] Backbone.Marionette CollectionView/CompositeView rendering
问题描述
此问题是此相关问题的扩展.
根据Derick的建议,我现在的数据格式正确.即我有一个Department
对象的集合,每个对象都有一个Users
的集合.
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
.
再次按照Derick的建议,我尝试呈现CompositeView
的CollectionView
Again following Derick's advice I'm trying to render a CollectionView
of CompositeView
's
我的收藏集视图如下
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
和我的Composite View
看起来像这样:
class UserCompositeView extends Backbone.Marionette.CompositeView
itemView: UserItemView
itemViewContainer: '#users'
如果我不重写appendHtml方法,则该视图将呈现,但仅呈现Department
模型的属性.它不会呈现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.
当我覆盖CollectionView
中的appendHtml
方法时,我可以传递一个模型(一个Department
对象)和一个users
的集合,但是它们之一或两者似乎是错误的对象类型因为Marionette bindTo
函数抱怨该对象没有'on'方法.
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.
我在做什么错了?
推荐答案
itemModel.get("users")
返回什么?
如果这将返回JavaScript数组或对象文字,则可能是问题.您必须传递一个有效的Backbone.Collection作为collection参数,而不仅仅是一个对象数组.
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"))
这篇关于Backbone.Marionette CollectionView/CompositeView呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!