7
votes

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?

1

1 Answers

19
votes

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