2012-02-10 21 views
0

我的,我想在一個web應用程序,顯示模式幾個列表,所以我創建了一個通用類,如下所示:Backbone.js的:房產在子類View是未定義

App.ModelListView = Backbone.View.extend({ 

    initialize: function() { 
     this.collection.bind('reset', this.addAll, this); 
     this.collection.bind('add', this.addOne, this); 
     this.collection.bind('remove', this.removeOne, this); 
    }, 

    addAll: function() { 
     this.removeAll(); 
     this.collection.each(this.addOne, this); 
    }, 

    ... 

}); 

對於每個模型類型,我子類App.ModelListView像這樣:

App.ProjectListView = App.ModelListView.extend({ 

    el: $("#project-list"), 

    collection: App.Projects, 

    listItemView: App.ProjectListItemView 

}); 

當我使用實例

App.ProjectMainListView = new App.ProjectListView(); 
視圖

我發現當App.ModelListViewinitialize函數執行時,'this.collection'是未定義的。爲什麼在我使用的子類中定義的值不是collection?我在這裏錯過了什麼?

回答

0

我想你會在定義你的類後在某處設置App.Projects。試着改變你的看法實例的創建以下::

App.ProjectMainListView = new App.ProjectListView({ 
    collection: App.Projects 
}); 

當時你定義您查看的時間,我敢打賭,你還沒有設置App.Projects

+0

你說得對!我將「App.Projects」的定義移至「App.ProjectMainListView」之前,並且一切都很好。應該抓住那個... – 2012-02-10 05:00:46