2012-04-09 20 views
0

我正在嘗試做一些非常簡單的事情。骨幹路由器as/en /#xyz而不是/#en/xyz

這裏是場景: 我有一個完整的網站,與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 

這真的光滑,正如我所提到用pushState的作品很好。問題是當我嘗試使用散列替代方法使用相同的路由器代碼時。

骨幹路由器似乎想做到這一點:

/#/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.js,除非我真的必須因爲未來的防禦因素。

任何人有任何想法?

回答

5

您需要使用root選項在Backbone History object:

引用文檔:

如果您的申請不被從根網址服務/域的,一定要告訴史哪裏根本是,作爲一個選項:Backbone.history.start({pushState: true, root: "/public/search/"})

你將不得不做一些代碼發生在你的着陸頁上,看看他們正在使用什麼語言,然後se請將它們發送到/[LANG CODE]/。然後在您的服務器上,您只需將/[LANG CODE]/的請求映射到.html文件。

在您的HTML文件中查看location.pathname變量,剝離第一個/[LANG CODE]/部分,並將其用作您的選項散列的root值。再次

/en/#whatever/etc 
/ru/#whatever/etc 
+0

嘿感謝你救了我tkone:

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

這將導致您的網址是!我將不得不開始發送你的鈔票,而不是點數---) – Alex 2012-04-10 11:18:20

+0

@亞歷哈哈沒有問題。很高興爲您服務! – tkone 2012-04-10 12:53:06