Using Composite Views with Backbone Marionette and Relational
Asked Answered
J

1

7

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?

Jacob answered 5/7, 2012 at 19:58 Comment(0)
D
19

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");
  }
});
Dania answered 6/7, 2012 at 1:9 Comment(1)
And you aren't going to get a much more authoritative answer than that :)Peg

© 2022 - 2024 — McMap. All rights reserved.