13
votes

I have a Backbone Router set up that seemingly works - the routes get triggered properly, the views update, etc. However, when I press the browser's "Back" button, the routes aren't triggered at all. Also, typing in a URL into the browser doesn't trigger the routers either. Is there some step I'm missing to bind the browser specific things to Backbone (Firefox 11).

Setup

var messageRouter = new MessageRouter({view: messageListView});
Backbone.history.start();

Trigger

Backbone.history.navigate("#/view/" + $(this).data("filter-type"), {trigger: true});

Router Code

var MessageRouter = Backbone.Router.extend({

    view : null, /* should always be overridden */

    initialize : function(options)
    {
        this.view = options.view;
    },

    routes : {
        "" : "default",
        "/view/:filter" : "filter",
        "camera" : "camera"
    },

    default : function() {
    },

    filter : function(filterString) {
        this.view.setFilter(filterString);
        this.view.rerender();
    },

    camera : function(cameraString) {
    }

});
3
usually I trigger without the "#" in the route. Is you're trigger script working? Also, what is the content of your filter? Are none of your routes working? Try taking off the "/" in from of the "/view/:filter" route.(in summary, your setup looks fine - I'm guessing there's a syntax error somewhere...) - eschwartz

3 Answers

1
votes

You should call router.navigate using the same path that you had already defined. ie:

trigger

messageRouter.navigate("/view/" + $(this).data("filter-type"), {trigger: true});

router

 routes : {
        "" : "default",
        "/view/:filter" : "filter",
        "camera" : "camera"
    },
1
votes

It might be the word default that's messing it up as it's a reserved word.

Either put quotes around the key 'default' in MessageRouter, or call it something else, like 'defaultRoute'.

'default': function() {},
defaultRoute: function() {}

http://jsfiddle.net/uwjDq/2/ -Seems to work OK here, including using the back button.

1
votes

this may be too late..but I had the exact same problem, and it turns out I accidently unbinded all events from window object, and that Backbone history was listening to events on window