4

Backbone.js provides a navigate(fragment, [options]) method for its Backbone.router objects, which does the following (per the documentation):

Whenever you reach a point in your application that you'd like to save as a URL, call navigate in order to update the URL. If you wish to also call the route function, set the trigger option to true. To update the URL without creating an entry in the browser's history, set the replace option to true.

Thus, simply calling appRouter.navigate('page'); would presumably change the URL to www.myapp.com/page without triggering the corresponding route. However, I'm finding that my router redirects to the URL and also triggers the page route, despite the fact that trigger=false by default.

Thus, the following code:

$(function(){
var AppRouter = Backbone.Router.extend({

    routes: {
        '': 'home',
        'page': 'page',
    },

    home: function() {
        window.app.navigate('page', {replace: true});        
        console.log('home route');    
    },

    page: function () {
        console.log('page route');
    },

});

window.app = new AppRouter();
Backbone.history.start({pushState: true});
});

produces the following console output when navigating to www.myapp.com:

> home route
> page route

when the expected console output should just be:

> home route

The method seems to be disobeying the default trigger option. Is this a bug in the implementation, or am I misunderstanding something?

Matm
  • 583
  • 1
  • 6
  • 17
  • does it work correctly with `trigger: false` set? – jakee Jun 26 '12 at 11:06
  • @jakee: No, `window.app.navigate('page', {trigger: false, replace: true});` still produces the same behavior. As does `window.app.navigate('page');`. – Matm Jun 26 '12 at 11:09
  • 1
    take off the `pushState: true` from the `Backbone.history.start` and try it with the original navigate call. I googled around and there might be a connection between `pushState: true` and `navigate` not working properly. – jakee Jun 26 '12 at 11:23
  • 1
    That did it. It correctly navigated to #page, while only firing the home route. Thanks! I'd really like to use this with `pushState: true` though. Is this in fact a bug in the implementation? – Matm Jun 26 '12 at 11:28
  • I don't know, my Googling produced only vague remarks about this from year 2011 and I had to patch it up by myself. I think it has to do with the pushState implementation in the js history api. But I'll add an answer and if you want to, you can accept it. – jakee Jun 26 '12 at 11:32

2 Answers2

2

Updating to the latest version of Backbone as of 6/28/12 (the master version on https://github.com/documentcloud/backbone) resolves the issue.

Matm
  • 583
  • 1
  • 6
  • 17
0

Take off the pushState: true from the Backbone.history.start and try it with the original navigate call.

I googled around for a solution to this, but all I got was vague remarks about the history API's pushState: true causing the Backbone Router's navigate function not to work properly. For an example of my Googlings this striked out article was about the best resource on the matter I could find.

jakee
  • 18,486
  • 3
  • 37
  • 42