Search code examples
backbone.jsmarionettebackbone-routing

How do I use a Router with an App in Marionette JS


I'm new to Marionette, and am stuck on the concept of how to establish the AppRouter with the App. I have code to start up my router, and it works:

var App = new Marionette.Application({
  onStart: function(options) {
    console.log('this works');
  }
});

I then create a controller:

var Controller = Marionette.Object.extend({
  main_route: function() {
    console.log('I NEVER GET HERE');
  }
});

My router:

var AppRouter = Marionette.AppRouter.extend({
  controller: new Controller,
  appRoutes: {
    'foo': 'main_route'
  }
});

I call App.start(), and it starts up with output, but my router code isn't called when I go to localhost:3000/#foo

How do I tell my Application to use my Router?


Solution

  • You need to call Backbone.history.start() to enable monitoring of 'hashchange' events. This should be done once your router and routes have been setup. Add it in the onStart function on your App.

    More detail at:

    http://backbonejs.org/#History