Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Composite Views with Backbone Marionette and Relational

I've been using Marionette for a couple of weeks and just discovered Backbone Relational so I'm trying to figure out how to integrate the two. Ideally, I would like to use a composite view to render data that is structured like this where each 'item' has its own item view:

list : {
  name : 'List 1',
  items : [
    item1 : {
      name : 'Item 1',
      id : 1
    },
    item2 : { ... }
    item3 : { ... }
  ]
}

Normally with composite views you need to have a collection of models that it will iterate through to render each item. With relational, I've just got one model (the list) and that model has a collection (items) within it. Is it possible to render this out using Marionette's views or do I need to use a plain Backbone view and handle the rendering and iteration myself?

like image 673
Will Hitchcock Avatar asked Jul 05 '12 19:07

Will Hitchcock


1 Answers

This is quite common, and easy to do. In your CompositeView definition, you can specify the collection to use in the initialize method.


Backbone.Marionette.CompositeView.extend({
  // ...

  initialize: function(){
    this.collection = this.model.get("childcollection");
  }
});
like image 112
Derick Bailey Avatar answered Nov 17 '22 00:11

Derick Bailey