【问题标题】:Get last item view from CompositeView in Marionette从 Marionette 中的 CompositeView 获取最后一个项目视图
【发布时间】:2013-09-01 05:18:02
【问题描述】:

是否可以从 Backbone CompositeView 获取最后一个 ItemView?我找到了很多文档来获取集合中的最后一个模型,但不是视图集合中的最后一个视图。

我想这样做的原因是我可以稍微不同地呈现表格中的最后一行。

以下是我现在正在使用的代码,它工作正常,但如果我能从创建和渲染它的 CompositeView 中获得正确的 ItemView,它会不那么“hacky”。它使用 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');
    }
});

谢谢!

【问题讨论】:

    标签: backbone.js marionette


    【解决方案1】:

    当您的收藏被提取时,您可以通过这种方式获取最后一项:

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

    Babysitter 插件为你提供了很多有用的方法:

    findByModelfindByCollectionfindByCustomfindByIndexfindByCid

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2015-12-19
    • 2014-10-12
    • 2015-11-28
    相关资源
    最近更新 更多