如何申请骨干路由器全路径,而不是一个散列 [英] How to apply backbone router for full path, not a hash

查看:101
本文介绍了如何申请骨干路由器全路径,而不是一个散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能性存在吗?我们的网站是不是一个页面,但所有的JS-COM文件pressed 的application.js 里面,我可以使用位置骨干路由器。路径解析?

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?

我尝试 Backbone.history.start(pushState的:真)。这对我的作品,但它是正确的?我只需要最初的分析,并不复杂的路线,并通过 Backbone.Router 重定向。

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.

推荐答案

您可以只用一个标准的路由器。当你实例,并启动历史对象,你可以设置哪根目录应该为它的基地使用。在这种情况下,似乎要使用/

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: '/'});

您需要设置Web服务器来提供HTML文件,只要任何目录称为服务器上(这样的骨干,没有护栏,将处理您的路由)。

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).

最后,在HTML文件中我有它运行在DOM已准备就绪,并拉起了一条走出的URL,并将其传递到导航的功能。

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});

这篇关于如何申请骨干路由器全路径,而不是一个散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆