最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to apply backbone router for full path, not a hash - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Mar 21, 2012 at 7:10 ValeriiVasin asked Mar 21, 2012 at 7:00 ValeriiVasinValeriiVasin 8,70611 gold badges59 silver badges79 bronze badges 2
  • What are you trying to accomplish? Can you have some examples and what you've tried already? – tkone Commented 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... – ValeriiVasin Commented Mar 21, 2012 at 11:52
Add a comment  | 

4 Answers 4

Reset to default 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});

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.

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.

I am using this:

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

to get absolute url of backbone app's root.

发布评论

评论列表(0)

  1. 暂无评论