2013-05-28 39 views
12

我正在使用Marionette CompositeView呈現html表。很棒!現在我想在集合中沒有記錄時顯示一條消息。我目前正在使用emptyView屬性來呈現此消息。但是,消息呈現在表包裝中,表列標題仍然可見。不完全是我想要的。理想情況下,我想隱藏/刪除表格並顯示空白記錄視圖,然後在添加記錄時顯示它。我正在努力尋找最好的方法來處理這個問題。那裏有什麼建議嗎?顯示在Marionette中的空視圖CompositeView

EmptyView = Marionette.ItemView.extend({ 
template: "#empty-template" 
}); 

SupportMemberView = Marionette.ItemView.extend({ 
template: "#member-template" 
}); 

SupportTeamView = Marionette.CompositeView.extend({ 
template: "#support-team-template", 
itemView: SupportMemberView, 
emptyView: EmptyView, 
itemViewContainer: 'tbody' 
}); 
+1

你可以張貼一些代碼? –

回答

5

你可以做的一件事是在你的emprty上查看使用onRender函數來隱藏表。這個函數在渲染函數之後被調用,所以你將能夠操縱dom來尋找你想要的方式。

+0

我開始考慮這個選擇,但是因爲我是Backbone/Marionette的新手,所以我會問,看看我是否缺少某些東西。 – Gentenator

+0

我認爲這是一個有效的場景,我認爲這個功能的原因就在於這種需求。 –

8

接受的答案強加空視圖和模板之間的依賴關係,這不正確。

我認爲一種替代方法是在合成視圖中使用動態模板。這是通過覆蓋基本的View getTemplate()方法來完成的。因此,您的複合視圖將被定義如下,假設你有機會獲得underscore.js庫或等同替換「_.isEmpty()」函數:

SupportTeamView = Marionette.CompositeView.extend({ 
getTemplate: function() { 
    if (_.isEmpty(this.collection)) { 
     return "#empty-template" 
    } else { 
     return "#support-team-template"; 
    } 
itemView: SupportMemberView, 
emptyView: EmptyView, 
itemViewContainer: 'tbody' 
}); 
+2

你的想法存在的問題是'getTemplate'只被調用一次,並且你將它綁定到一個動態的東西 - 集合的內容。所以這個想法會被卡在收藏永遠不會改變的事實上。 –

相關問題