【发布时间】:2015-11-10 22:16:13
【问题描述】:
我是 Marionette.js 的新手,在渲染集合视图时似乎遇到了一些问题。
我在尝试显示视图时收到以下控制台错误消息:Uncaught TypeError: Cannot read property 'toJSON' of undefined。
似乎该集合没有绑定到子视图。集合视图的实例看起来不错,这意味着我看到了 childView 属性和带有获取模型的集合属性。关于如何构建集合视图的文档似乎非常简单,所以不确定我缺少什么。谢谢。
Child view:
var UserProfile = Marionette.ItemView.extend({
template: '',
initialize: function() {
this.template = Marionette.TemplateCache.get("#userprofile");
},
render: function() {
console.log(this.collection); //undefined
var data = this.collection.toJSON(); //error message here
this.$el.html(this.template({data:data}));
}
});
//Collection view:
var UserView = Marionette.CollectionView.extend({
childView: UserProfile
});
// Fetch collection and show:
var myQuery = new Parse.Query(app.Models.User);
myQuery.limit(200).containedIn(option, uiArray);
var Contacts = Parse.Collection.extend({
model: app.models.User,
query: myQuery
});
var contacts = new Contacts();
contacts.fetch().then(function(contacts) {
var userview = new UserView({collection:contacts});
app.regions.get("content").show(userview);
})
【问题讨论】:
标签: javascript backbone.js marionette collectionview