gpt4 book ai didi

javascript - 主干路由器为/en/#xyz 而不是/#en/xyz

转载 作者:行者123 更新时间:2023-11-28 13:52:15 27 4
gpt4 key购买 nike

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

场景如下:我有一个整个网站与支持推送状态的浏览器配合得很好。该网站的工作基础是该语言是“实际页面”,例如:

/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

正如我提到的,它非常灵活,并且与 Pushstate 配合得很好。问题是当我尝试将相同的路由器代码与哈希替代方案一起使用时。

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

/#/en/whatever/etc = bad because it's not based correctly
/#/ar/whatever/etc = and so on

我想要它做的是:

/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

但是如果不调整 Backbone 源来实现这一点,我找不到方法。我有点反对改变backbone.js,除非出于面向 future 的考虑我真的必须这么做。

大家有什么想法吗?

最佳答案

您需要使用Backbone History object:中的root选项

引用文档:

If your application is not being served from the root url / of your domain, be sure to tell History where the root really is, as an option: Backbone.history.start({pushState: true, root: "/public/search/"})

您必须在目标网页上执行一些代码,查看他们使用的语言,然后将它们发送到 /[LANG CODE]/。然后在您的服务器上,您只需将对 /[LANG CODE]/ 的请求映射到您的 .html 文件。

在您的 HTML 文件中,查看 location.pathname 变量,去掉第一个 /[LANG CODE]/ 部分,并将其用作您的 root 选项哈希值。

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

这将导致您的网址为:

/en/#whatever/etc
/ru/#whatever/etc

关于javascript - 主干路由器为/en/#xyz 而不是/#en/xyz,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10073509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com