0
votes

I am exploring the BBCloneMail demo application for MarionetteJS, but I am not seeing how the events are triggering the rendering actions. I saw some global 'show' event here:

https://github.com/marionettejs/bbclonemail/blob/master/public/javascripts/bbclonemail/components/appController.js#L25

show: function(){
  this._showAppSelector("mail");
  Marionette.triggerMethod.call(this, "show");
},

But I don't see, where/how the Marionette.triggerMethod results into rendering the Mail component. I was trying to call the triggerMethod for my case, but I get a 'cannot call apply for undefined'. Why is the call above working for the BBcloneMail application.

trigger Method

The Application controller for my case:

MA.AppController = Marionette.Controller.extend({

  initialize: function(){
    _.bindAll(this, "_showGenres");
  },


  show: function() {
    if (MA.currentUser) {
      MA.navbar.show(new MA.Views.Items.LogoutNavbar({model: MA.currentUser}));
    }
    else
    {
      MA.navbar.show(new MA.Views.Items.LoginNavbar());
    }
    this._showGenres();
  },

  _showGenres: function() {
    var categoryNav = new MA.Navigation.Filter({
      region: MA.filter
    });
    this.listenTo(categoryNav, "genre:selected", this._categorySelected);
    categoryNav.show();
    MA.main.show(MA.composites.movies);
  },

  showMovieByGenre: function(genre){
    var movies = new MA.Controllers.MoviesLib();
    that = this;
    $.when(movies.getByCategory(genre)).then(that._showMovieList);

    Backbone.history.navigate("#movies/genres/" + genre);
  },

  _showMovieList: function(movieList){
    var moviesLib = new MA.Controllers.MoviesLib({
      region: MA.main,
      movies: movieList
    });
    Marionette.triggerMethod.call(this, "show");

  }
});

I init the application controller in a init.js with:

app = new MA.AppController();
1
Can you post your controller code?nemesv

1 Answers

3
votes

Looking at the source for triggerMethod, this is a way of both triggering an event (the string being passed in), and additionally (if it exists) running a method on the object that has an 'on' prefix.

In your case the error relates to line 560, specifically that there is no method apply on undefined. Based on the code its (in your case) trying to call the equivilent of this.trigger('show') - but AppController doesn't have a method called trigger.

In which case I'm guessing that in the BBCloneMail example this (being bassed into triggerMethod.call) is not actually the controller, but instead the view that is to be shown.