Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 515
Achim Koellner Avatar asked Aug 22 '12 09:08

Achim Koellner


2 Answers

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

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

like image 193
Erez Rabih Avatar answered Nov 10 '22 07:11

Erez Rabih


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', 
},
like image 27
mu is too short Avatar answered Nov 10 '22 07:11

mu is too short