Pergunta

I am following along with a two tutorials to use nested CompositeViews with my Backbone application (http://lostechies.com/derickbailey/2012/04/05/composite-views-tree-structures-tables-and-more/ and http://davidsulc.com/blog/2013/02/03/tutorial-nested-views-using-backbone-marionettes-compositeview/) but I'm having trouble adapting it to my situation where two models are related through storing the id of one model in the other.

I'm trying to achieve a list of movies with each movie listing associated actors below it.

Here's what I currently have which is displaying the list of movies but does not render any actor lists:

MovieApp.module("ActorsApp.List", function(List, MovieApp, Backbone, Marionette, $, _){
  List.Controller = {
    listActors: function(){
      var actors = MovieApp.request("actor:entities");
      var movies = new Backbone.Collection;
      var allMovies= MovieApp.request("movie:entities");

      actors.each(function(actor) {
        movieId = actor.get("movieId");

        if (!movies.get(movieId) ){
          console.log("Movie not found. Adding to list list.");
          movie = allMovies.get(movieId);
          selectedActors = actors.where({ movieId: movieId });
          console.log("Selected Actors: ", selectedActors);
          movie.actors=selectedActors;
          movies.add(allMovies.get(movieId));

        }
        else {
          console.log("Movie found in list. Skipping.")

        }
      });

      var moviesListView = new List.Movies({
        collection: movies
      });

      MovieApp.mainRegion.show(moviesListView);
    }
  }
});



  /* list_view.js */


PassCake.module("SitesApp.List", function(List, PassCake, Backbone, Marionette, $, _){
  List.Actor = Marionette.ItemView.extend({
    tagName: "li",
    template: "#actor-list-item",

    initialize: function(){
      console.log("Actor ItemView: " + this.model.get('title'));

    }

  });

  List.Movie = Marionette.CompositeView.extend({
    tagName: "li",
    template: "#actor-movie-list-item",
    itemView: List.Actor,
    initialize: function(){

       console.log("Actors: ", this.model.actors);

        actors = this.model.actors;

        /* The following line throws an error saying TypeError: obj[implementation] is not a function obj[implementation](name, callback, this);
           Outputting this.model.actors seems to work but it isn't working when assigned to this.collection. 
        */
        this.collection = actors;

        console.log("Movie view initialized");
    }



  });

  List.Movies = Marionette.CompositeView.extend({
    tagName: "div",
    template: "#actor-movies-list",
    itemView: List.Movie,
    itemViewContainer: "#actors-movies"
  });

});

I'm pretty new to Backbone so I'd love to know ways to make this cleaner as well. Thanks for taking a look!

Foi útil?

Solução

First, use var more, you are declaring a lot of global variables.

My guess is that the line this.collection = actors;, actors is not a Backbone Collection it is probably an array, assuming that actors.where({ movieId: movieId }); returns an array.

Try changing the line to this.collection = new Backbone.Collection(actors)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top