CompositeView和ItemView,如何处理两个视图和模板

CompositeView and ItemView, how to handle two views and templates?

本文关键字:视图 两个 ItemView 何处理 CompositeView 处理      更新时间:2024-01-18

我有以下CompositeView(1)
我想知道,对于MyCollection的每个模型,渲染两个模板和视图的最佳方式是什么(2)。


(1)

var MyCompositeView = Marionette.CompositeView.extend({
    template: myTemplate,
    itemView: myView,
    collection: new MyCollection(),
    initialize: function () {
        this.collection.fetch();
    },
    appendHtml: function (collectionView, itemView) {
        collectionView.$el.find('ul').append(itemView.el);
    }
});

(2)

    appendHtml: function (collectionView, itemView1, itemView2) {
        collectionView.$el.find('ul').append(itemView1.el);
        itemView.$el.append(itemView2.el);
    }

实现目标的另一种方法,结果应该是一样的,就是在Marionette.ItemView:中定义onRender funciton

Marionette.ItemView中的代码应该如下所示:

    onRender: function () {
        var itemView2 = new ItemView2();
        itemView2.render();
        this.$el.append(itemView2.$el);
    }