2013-09-01 29 views
6

是否有可能得到一個骨幹CompositeView中的最後一個ItemView控件?我發現了很多用於獲取集合中最後一個模型的文檔,但沒有找到集合中的最後一個視圖。得到最後一個項目視圖,木偶

的原因,我想做到這一點,所以我可以呈現在一個表中的最後一行略有不同。

以下是我現在使用的代碼,它工作正常,但它是不那麼「哈克」如果我能得到從創建和渲染它CompositeView中正確ItemView控件。它使用jQuery來搜索CompositeView所包含的DOM的最後一個元素的整個部分,然後處理該元素。

B.ListControl.View = Backbone.Marionette.CompositeView.extend({ 
    itemView: ..., 
    itemViewContainer: ..., 
    template: ..., 
    // ON RENDER 
    onRender: function(){ 
     // Add button to the last element in the list 
     this.$el.find('div:last').text('Custome stuff goes here'); 
    } 
}); 

謝謝!

回答

10

當你收集獲取,你可以通過這種方式得到最後一個項目:

this.children.findByIndex(this.children.length - 1); 

保姆插件提供了許多有用的方法給你:

findByModelfindByCollectionfindByCustomfindByIndexfindByCid

+0

太棒了,非常感謝! –

+0

Backbone.BabySitter文檔查找從'ChildViewContainer'(其'.children'是的一個實例)的觀點:https://github.com/marionettejs/backbone.babysitter#retrieving-views – alxndr

+0

N.B.木偶的'CompositeView'有一個叫做'.childViewContainer'屬性,但它是不相關的:木偶希望它是,它會用它來確定在哪裏把它使得'childView'類的實例的jQuery選擇字符串。 https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.compositeview.md#compositeviews-childviewcontainer – alxndr