Achim Koellner
Achim Koellner

Reputation: 923

Backbone Route is not called

I have a working app using Backbone 0.5.3, which is no longer working using backbone 0.9.2.

I identified that Router.navigate() doesn't call my route for some reason.

Here's my Router:

var Router = Backbone.Router.extend({
    routes: {
      '/mypage': 'mypage', 
    },

    mypage: function() { 
      // show page ...
   }
});

Calling the route manually like so works fine:

Router.mypage()

I also tried to overwrite backbone's .navigate method to debug my app ...

var Router = Backbone.Router.extend({
    routes: {
      '/mypage': 'mypage', 
    },

    navigate: function(fragment, options) {
      console.log("navigate called");
      Backbone.history.navigate(fragment, options);
    },

    mypage: function() { 
      // show page ...
   }
});

... it seems that .navigate is called but ...

Backbone.history.navigate(fragment, options);

... just doesn't call the route.

I'm using PushState, here's my initial call:

Backbone.history.start({ 
  root: '/',
  pushState: true,
  silent: true
});

Already tried it without the root and silent parameters - no success.

Again: This works using Backbone 0.5.3.

Thanks to everyone leaving a reply!

Achim

Upvotes: 5

Views: 1963

Answers (3)

Erez Rabih
Erez Rabih

Reputation: 15788

You have to set the trigger option for the navigate method, e.g:

Router.navigate("/mypath", {trigger: true})

Upvotes: 3

mu is too short
mu is too short

Reputation: 434585

From the fine manual:

extend Backbone.Router.extend(properties, [classProperties])

Get started by creating a custom router class. [...] Note that you'll want to avoid using a leading slash in your route definitions:

I'm guessing that you simply need to remove the leading slashes from your routes, for example:

routes: {
  'mypage': 'mypage', 
},

Upvotes: 2

SoWa
SoWa

Reputation: 314

You must create instance of Router.

var router = new Router();
router.navigate(...);

Upvotes: 0

Related Questions