Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply backbone router for full path, not a hash

Does that possibility exist? Our site is not one page, but all js-files compressed inside of application.js, can I use backbone router for location.path parsing?

I try Backbone.history.start(pushState: true). It works for me, but is it correct? I need just initial parsing, not complicated routes and redirects via Backbone.Router.

like image 618
ValeriiVasin Avatar asked Mar 21 '12 07:03

ValeriiVasin


1 Answers

You can just use a standard router. When you instantiate it and start the history object you can set what the root directory it should use as its base. In this case it seems you want to to use '/'

var MyRouter = Backbone.Router.extend({
    routes: {
        "application/": "somefunc"
    }
}

var app = new MyRouter();
Backbone.history.start({pushState: true, root: '/'});

You'll need to set your web server to serve up your HTML file whenever any directory is called on your server (so backbone, not rails, will handle your routes).

Finally, in the HTML file I have a function which runs on Dom ready and pulls the path out of the URL and passes it to navigate.

var path = location.pathname;
app.navigate(path, {trigger: true});
like image 99
tkone Avatar answered Oct 12 '22 13:10

tkone