Using the Backbone.js router to navigate through views modularized with require.js

In case anyone else is looking for a solution to this problem like I was, I’m posting what I ended up doing. If you’re using the boilerplate backbone.js, then you will have an initialize() function in router.js. I modified my initialize() function to look like the following:

initialize = function () {
  var app_router;
  app_router = new AppRouter();

  // Extend the View class to include a navigation method goTo
  Backbone.View.goTo = function (loc) {
    app_router.navigate(loc, true);
  };

  Backbone.history.start();
};

Due to backbone.js’s particular flavour of inheritance, this allows allows me to call MyView.goTo(location); from within any of my views.

Leave a Comment

tech