Arbejdsglæde Arbejdsglæde - 11 days ago 5
Javascript Question

How to ignore hash in URL on Backbone.history.start

I have an URL like mysite.com/en/bla-bla/bla/page#someid
Browser should scrolling window to some page location if find someid in hash URL.

Additionally, I have backbone on page, if user opens page with some hash value in URL backbone failed to If no defined route matches the current URL. Backbone can't match #someid as a path.

var result = Backbone.history.start();
if (!result) {
console.log("If no defined route matches the current URL");
}


How to solve it ?

Answer

You can add a catch all route:

splat parts *splat, which can match any number of URL components.

var Router = Backbone.Router.extend({
    routes: {
        '*catchall': 'homeRoute',
        // any route defined further down takes precedence on the ones before.
        'real-route/:id': 'realRoute',
    },
    homeRoute: function() { /*...*/ },
    realRoute: function(id) { /*...*/ }
});

Define any route you want Backbone to handle, then let any undefined route be caught by the splat.