骨干路由器为/ en /#xyz而不是/ #en/xyz

Ale*_*lex 0 javascript backbone.js pushstate

我正在尝试做一些非常简单的事情.

这是一个场景:我有一个完整的网站与支持pushstate的浏览器一起工作.该网站的工作原理是语言是"实际页面",例如:

/en/whatever/etc  = index.en.php with  with english headers and encoding & tweaks
/ar/whatever/etc  = index.ar.php with  with arabic headers and encoding & tweaks
/ru/whatever/etc  = index.ru.php with  with russian headers and encoding & tweaks
Run Code Online (Sandbox Code Playgroud)

正如我所提到的,它非常光滑,与pushstate很好地配合使用.问题是当我尝试使用相同的路由器代码和哈希替代方案时.

Backbone的路由器似乎想要这样做:

/#/en/whatever/etc = bad because it's not based correctly
/#/ar/whatever/etc = and so on
Run Code Online (Sandbox Code Playgroud)

我想要它做的是:

/en/#whatever/etc
/ar/#whatever/etc
/ru/#whatever/etc
..and so on

or even:

/en/#/whatever/etc
/ar/#/whatever/etc
/ru/#/whatever/etc
..and so on
Run Code Online (Sandbox Code Playgroud)

但是,如果没有调整主干源来实现这一点,我找不到方法.我有点反对改变backbone.js,除非我真的不得不因为未来的防范因素.

有人有什么想法?

tko*_*one 5

您需要使用Backbone 对象中root选项:History

引用文档:

如果您的应用程序未从您的域的根URL /提供服务,请务必告诉历史记录根目录的位置,作为选项: Backbone.history.start({pushState: true, root: "/public/search/"})

您将不得不在着陆页上执行一些代码,这些代码会查看他们正在使用的语言,然后将其发送给/[LANG CODE]/.然后在您的服务器上,您只需将请求映射/[LANG CODE]/到.html文件.

在HTML文件中查看location.pathname变量,剥离第一/[LANG CODE]/部分并将其用作rootoptions散列的值.

var lang = "/"+_.first(location.pathname.split('/')+"/";
Backbone.history.start({pushState: true, root: lang}); // if you're using pushState, otherwise omit or set to false
Run Code Online (Sandbox Code Playgroud)

这会导致您的网址为:

/en/#whatever/etc
/ru/#whatever/etc
Run Code Online (Sandbox Code Playgroud)