2013-03-01 69 views
5

我遇到了問題:我的CollectionView未呈現我的ItemViews。我將一個集合從佈局傳遞到集合視圖。我取的CollectionView集合:木偶集合視圖,提取集合不會觸發事件

在佈局:

// Create a a collection for the view 
    this.articles = new Articles(null, { 
     organizationId : this.model.organizationId, 
     projectId : this.model.id 
    }); 

    var articlesView = new ArticleCollectionView({ collection : this.articles}); 
    this.articlesRegion.show(articlesView); 

在的CollectionView:

define([ 
    'marionette', 
    'templates', 
    'i18n!nls/projectDashboard', 
    'views/projectDashboard/ArticleItem' 
], function (Marionette, templates, msg, ArticleItemView) { 

    return Marionette.CollectionView.extend({ 

     initialize : function() { 
      this.listenTo(this.collection, "reset", this.render); 
      this.collection.fetch(); 
     }, 

     itemView : ArticleItemView 

    }); 

}); 

在ItemView控件:

define([ 
    'marionette', 
    'templates', 
    'models/Article' 
], 
function (Marionette, templates, Article) { 

    return Marionette.ItemView.extend({ 

     initialize : function() { 
      console.log('itemviewrender'); 
     }, 

     template : templates.projectDashboard.articleItem 
    }); 

}); 

一般的設置工作中。我找到了一種方法來實現這個功能:在佈局中獲取集合,並在成功回調中顯示區域中的CollectionView

但是,在集合collectionView上添加監聽器失敗。不會觸發事件的命令性和聲明聽衆喜歡

this.collection.on('reset', this.render, this); 

collectionEvents : { 
    'reset' : 'render' 
} 

我只是想重新呈現集合視圖與它的項目視圖如果收集被取出。我相信我錯過了一些東西。任何幫助表示讚賞!

更新:我發現了一些有趣的事情: 我已經說過,如果我在佈局中獲取集合,並在成功回調中創建collectionView,它將起作用。有趣的是:如果我通過抓取的集合,聽衆也會工作。我通過initialize再次呼叫this.collection.fetch()來觸發它們。然後,重新渲染的作品。它必須是來自佈局收集過程的東西。

+0

哪裏有約束力的事件?不應該在'CollectionView'初始化方法中嗎?另外嘗試使用'listenTo'而不是''on' – neebz 2013-03-01 12:25:35

+0

它們在collectionView中,我應該在CollectionView中添加一個以便更好地理解我的猜測,dovenst使用listenTo要麼 – pfried 2013-03-01 12:29:09

+0

這很有趣。根據文檔,似乎你不需要像Marionnette自動爲你自動綁定事件,請參閱:https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.collectionview.md# collectionview-render。在這種情況下,它似乎是一個奇怪的問題, – neebz 2013-03-01 12:56:13

回答

2

您想使用的CollectionEvent

下面是從木偶文檔

Marionette.CollectionView.extend({ 
    collectionEvents: { 
    "sync": "render" 
    } 
}); 

在渲染會被觸發時集合已完全從後端

填充的例子爲例

modelEvents and collectionEvents

相關問題