2015-11-10 48 views
0

我與Marionette.js相當新,似乎在呈現集合視圖時遇到了一些麻煩。Marionette.js集合在集合視圖中未定義

嘗試顯示視圖時,我收到以下控制檯錯誤消息:Uncaught TypeError: Cannot read property 'toJSON' of undefined

看來,集合沒有綁定到子視圖。集合視圖的實例看起來OK,這意味着我看到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); 
}) 

回答

2

你正在問一個ItemView中的「集合」。 ItemView使用一個項目。所以寫這樣的東西是有道理的:

render: function() { 
    console.log(this.model); //undefined 
    var data = this.model.toJSON(); //error message here 
    this.$el.html(this.template({data:data})); 
    } 
}); 
+0

這似乎工作。我想這將允許我使用ItemView,當我只想在應用程序的其他位置呈現單個模型時? – user2232681