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

Question!

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


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


Answers

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

Your code in Marionette.ItemView should look like this:

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

        itemView2.render();
        this.$el.append(itemView2.$el);
    }
By : antonjs


This video can help you solving your question :)
By: admin