11

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.

2
  • What are you trying to accomplish? Can you have some examples and what you've tried already?
    – tkone
    Mar 21, 2012 at 11:31
  • for example, when I open path /testimonials I want to accomplish some function (in the router). It's all I need, cuz all my modules are inside of application.js (it's rails app). And I want to know, what I should execute... Mar 21, 2012 at 11:52

4 Answers 4

13

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});
7
  • There is an issue with IE... IE doesn't have pushState and Router always going to #my-path instead of /my/path... Is it possible to fix it? Apr 2, 2012 at 9:09
  • 1
    @InviS No. pushState only works on browser that support the HTML5 History object. [Backbone will specifically revert to the hash solution[(documentcloud.github.com/backbone/#History-start) on unsupported browsers. The problem with browsers that don't support pushState is that the browser will make a new request when you give it a valid URL through modifying the location object.
    – tkone
    Apr 2, 2012 at 15:33
  • But I don't need pushState at all... I'm using it to locate on what page I am... Is there any method to do it? how to set default navigation from root path /, not #. Apr 2, 2012 at 19:16
  • @InviS no that would require pushState. The instant a router takes over your page, it requires either changing location.hash or using the History object. If you aren't using a router to handle location, why not just use a View and it "control" your page.
    – tkone
    Apr 2, 2012 at 20:33
  • I explain: I have a lot of pages (Rails App) and I am using Sprockets, and all of javascript is inside of application.js. I need a router to know what page I visit. All I need is - get initial url and detect what route is. And execute some function. But Backbone router works with hashchange by default. pushState works perfect, but IE.... Apr 3, 2012 at 7:12
1

You'll need to write your own router, but fortunately that's pretty easy. Here's one I just wrote for a site I'm working on:

var routes = {
  "terms": "terms",
  "privacy": "privacy",
  "password-reset": "reset"
};

var path = window.location.pathname.replace("/", "");
var page = routes[path];
if (!page) {
  page = "404";
}

I didn't need any of the fancy matching rules that Backbone provides, but it wouldn't be too hard to extend this idea with regular expressions. Or perhaps someone could write up a little module that uses Backbone's matching logic but doesn't do the hash/pushState redirection.

1
  • it's quite simple example. Usually, routes are not so simple and you could find a need to provide route like this: /hello/world/:post/:date Jul 15, 2012 at 5:48
0

I had the same IE issue, there is a simple sollution. Download modernizr and include it.

Then do:

Backbone.history.start({ pushState: Modernizr.history });

This should do the trick.

1
  • 1
    Backbone automatically detects if the browser has pushState and will fall back to hashes if pushState is not supported, so this is unnecessary -- just set pushState to true in history.start. Aug 25, 2013 at 20:12
0

I am using this:

window.location.href.replace(Backbone.history.getFragment(), '');

to get absolute url of backbone app's root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.