Using Marionette CollectionView to create multiple

2019-08-10 04:18发布

问题:

I have a Marionette.CollectionView that for certain items (models) need to create 2 views to run on the model, can Marionette.CollectionView.buildItemView return 2 views?

EDIT:

I don't want to create some another WrapperItemView, I already have some solution running right now but I want to make it more standart.

I want my code to be look like this, is there a simple way to make it work?

  buildItemView: function(item){
    // create 2 views based on item type 
    return [view1, view2];
  },
  appendHtml: function(collectionView, itemView, index){
    if (itemView.type === "X" ) 
        collectionView.$el.find(".A").append(itemView.el);
    if (itemView.type === "Y" ) 
        collectionView.$el.find(".B").append(itemView.el);
  }

回答1:

why dont your itemview acts as father of the second view, you can create the second view in the initialize function of the item view.

Edit

I would still create a perent view this time I will make it the parent of your 2 views, your the collection will create the parent and the parent will create the two views inside of it.

this way is more natural to me.