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

时间:2022-11-24 11:21:47

I have the following CompositeView(1).
I am wondering what is the best way, for each model of MyCollection, to render two templates and views in order to make something like that(2).

我有以下CompositeView(1)。我想知道对于每个MyCollection模型,最好的方法是渲染两个模板和视图,以便制作类似的东西(2)。


(1)

(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)

(2)

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

1 个解决方案

#1


2  

Another way to achieve your goal, the result should be the same, is to define onRender funciton in Marionette.ItemView:

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

Your code in Marionette.ItemView should look like this:

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

    onRender: function () {
        var itemView2 = new ItemView2();

        itemView2.render();
        this.$el.append(itemView2.$el);
    }

#1


2  

Another way to achieve your goal, the result should be the same, is to define onRender funciton in Marionette.ItemView:

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

Your code in Marionette.ItemView should look like this:

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

    onRender: function () {
        var itemView2 = new ItemView2();

        itemView2.render();
        this.$el.append(itemView2.$el);
    }