2017-02-13 50 views
2

我使用Backbone和Marionette。如何在Backbone中獲取網址參數?

我有一個鏈接<a>標籤我傳遞了很少的參數,我怎麼能在其他頁面使用Backbone提取這些值?

<a href="http://localhost.com:8080/help/?name=matth&age=25&[email protected]">View Details</a> 

地址欄網址:

http://localhost.com:8080/help/?name=matth&age=25&[email protected] 44 

使用PHP,這很簡單:

$Url = $_GET['state']."#".$_GET['city']; 

如何將我的主幹應用程序中實現呢?

+0

[使用querystring導航路線]的可能重複(http://stackoverflow.com/questions/11671400/navigate-route-with-querystring) –

回答

3

如果路由與這樣的定義:只需在方法(簽名定義骨幹路由器在其中

'help/:name&:age&:email' : 'help' 

然後你就可以訪問這些PARAMS在help功能 ),

help: function(name, age, email) { 
    // do whatever you want with the params 
} 

在你的情況,這會給你PARAMS這樣的:

name="XXX" age="XXX" 

所以正確的路由將

'help/?(name=:name)(&age=:age)(&email=:email)' : 'help' 

凡括號使部分可選。

Backbone docs

路由可以包含參數部分,:param


注意順序很重要,以下網址就不會觸發路線回調。注意emailage params的位置。

help/?name=test&email=test%40example.com&age=6 

爲了觸發路由,不考慮PARAMS和他們的訂購數量的,看看在路由功能如何parse the query string,但不會總是奏效。

+1

請注意,使用此路線時,參數順序非常重要,且無可選項。它們可以用括號'help /?(name =:name)(&age =:age)'作爲可選項,但是不同的順序不會觸發路由。 –

+0

括號應該在路由字符串中,而不是在URL中。我的意思是像'help /?name = test&email = test%40example.com&age = 6'這樣的URL即使是有效的URL也會失敗,因爲查詢字符串參數順序與設計無關。 –

+1

通過編輯啓發我們。 –