1

So I have an object that looks something like this

{
  first_name: 'Matt',
  last_name: 'Damon',
  movies: [{name: 'mov_name1', director: 'Sven'}, {name:'mov_name2', director: 'Joe'}]
}

There is an endpoint that will return this, and I will use fetch to get this data and put it in a model or ItemView. (I have to use fetch because I really want a collection of these objects, so I'll call fetch on the collection.) However, the representation of the object should not be a model alone, it should be a model (or ItemView), and the movies attribute should be a collection, since when I display the row for this object it should be a CompositeView with a model and a collection. My question is how do I convert this object representation to a model and collection after a fetch called on the model?

Sorry it's a bit confusing to explain...

user396004
  • 285
  • 1
  • 4
  • 14

2 Answers2

2

do a fetch on your collection and then for each element create a model and a collection that you can pass then to a compositeView

something like this

  mycollection = new MyCollection();
  mycollection.fetch();
  mycollection.each(function(item){
      var actorModel = new ActorModel({firstName : item.get('first_name'),
                               lastname: item.get('last_name')});
      var moviesCollection = new MoviesCollection(item.get('movies'));
      var xCompositeView = new XCompositeView({model:xmodel,
                                               collection:moviesCollection});
  });

this way your Compositeview gets a model and a collection and you can render them properly using an itemView for each movie.

hope this helps.

Rayweb_on
  • 3,727
  • 20
  • 24
  • Yes, this is the difference between `CollectionView` and `CompositeView`. CompositeView can also render its own non-repetitive items using a model in addition to the collection. – Pawan Jul 16 '13 at 09:15
  • Thanks for the advice! Doesn't this seem to be doing extra work, though? Is there a cleaner way to represent this? I'll keep brainstorming, was just wondering if there's a convention when handling associations. – user396004 Jul 16 '13 at 17:19
  • Also, I was hoping that the collection of actors (and their movies) would be in a CollectionView, so iterating over the objects to create a CompositeView might not be ideal... What if in the initialization of the compositeView it had code to extract the collection from that actor's movie array, similar to this http://stackoverflow.com/questions/12163118/nested-collections-with-backbone-marionette. I figured it would be good practice since Derick Bailey posted the answer haha. What do you think? – user396004 Jul 16 '13 at 18:03
0

If its not a problem to use another framework, there is one called backbone-relational. It is designed to handle the relations between models. See the zoo example in Backbone-Relational

In the example there is a Zoo model and models has a Animal Collection which consist of animal models. With a single fetch on Zoo model, you will have the animal collection as Backbone collection instead of array of plain objects.

anil
  • 61
  • 2
  • Sweet, thanks I'll look into this. I've never heard of this before! Do you think it will work with Marionette? – user396004 Jul 16 '13 at 17:20
  • yes it works. I mean relational model simply extends the Backbone.Model so you can safely use it within your Marionette views – anil Jul 17 '13 at 06:21