2017-02-13 49 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

通过编辑启发我们。 –