javascript Backbone.js 路由匹配可选参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9649047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone.js Router Matching Optional Parameters
提问by abraham
I'm trying to create a backbone router that can match optional parameters.
我正在尝试创建一个可以匹配可选参数的骨干路由器。
Consider the following code:
考虑以下代码:
routes: {
'/jobs' : 'jobs',
'/jobs/p:page' : 'jobs',
'/jobs/job/:job_id' : 'jobs',
'/jobs/p:page/job/:job_id' : 'jobs'
}
jobs: function(page, job_id){
// do stuff here ....
}
If I navigate to URL abc.com/#/jobs/p104/the pageparameter will be 104. However, if navigate to abc.com/#/jobs/job/93, the job_idparameter is undefinedbut the pageparameter is 93.
如果我浏览到URL abc.com/ #/职位/ P104 /的页面参数将是104。然而,如果导航到abc.com/ #/求职/招聘/ 93,该JOB_ID参数不确定,但页面参数为93。
So Backbone's router basically matches the routes hash parameters by order and not by name.
所以 Backbone 的路由器基本上是按顺序而不是按名称匹配路由散列参数。
I know the solution would be to use a *splat and split the parameters with regex, but I can't seem to get the regex part to work (my regex is pretty rusty). Can someone please help?
我知道解决方案是使用 *splat 并用正则表达式拆分参数,但我似乎无法让正则表达式部分工作(我的正则表达式非常生疏)。有人可以帮忙吗?
If there's a better solution than using *splat, can someone please share?
如果有比使用 *splat 更好的解决方案,有人可以分享吗?
回答by abraham
Instead of messing with regexes it would be easier and less error prone just to have a second function. You will probably have to bindAll this in an initialize function for this.jobs to work.
而不是搞乱正则表达式,它会更容易,更不容易出错,只是拥有第二个功能。您可能必须在初始化函数中绑定所有 this 才能使 this.jobs 工作。
routes: {
'/jobs' : 'jobs',
'/jobs/p:page' : 'jobs',
'/jobs/job/:job_id' : 'jobsId',
'/jobs/p:page/job/:job_id' : 'jobs'
},
jobs: function(page, job_id){
// do stuff here ....
},
jobsId: function(page, job_id){
this.jobs(undefined, job_id
}
回答by Scott Weaver
try this (single regex, works on all your input formats)
试试这个(单个正则表达式,适用于所有输入格式)
var re = /\/jobs(?:\/p(\d+))?.*?(?:\/(\d+)|$)/;
var matches = 'abc.com/#/jobs/p104/4'.match(re);
var page=false;var job=false;
if (matches != null) {
var page = matches[1] || false;
var job = matches[2] || false;
};
alert("page:" + page + ",job:" + job)
**matches first the pNNN segment if it is there, and uses a non greedy quantifier with dot .
that can eat anything .*?
to crawl up the string one by one, so that the second group (just /NNN) will also match if present. the (?: exp )
are non-capturing groups, they group, but they don't "remember" anything.
**首先匹配 pNNN 段(如果存在),并使用.
可以吃任何东西的带点的非贪婪量词.*?
一个接一个地爬上字符串,以便第二组(仅 /NNN)也将匹配(如果存在)。这些(?: exp )
是非捕获组,它们分组,但它们不“记住”任何东西。
回答by rjz
If you're going to be doing a lot of these, you might glance at this patternfor wrapping them up tidily. For just these, though, the following regexs ought to do it:
如果你打算做很多这样的事情,你可能会看一眼这个模式,把它们整齐地包装起来。但是,对于这些,以下正则表达式应该这样做:
/\/jobs\/p(\d+)/ => /jobs/pXXX
/\/jobs\/p(\d+)\/job\/(\d+)/ => /jobs/pXXX/job/XXX
You can then use String.match
with the splats you retrieve to extract the url fragments in question. Using strings in place of a splat
variable:
然后String.match
,您可以使用检索到的 splats 来提取有问题的 url 片段。使用字符串代替splat
变量:
var matches = '/jobs/p18'.match(/\/jobs\/p(\d+)/);
var page = matches[1];
var matches = '/jobs/p4/job/6/'.match(/\/jobs\/p(\d+)\/job\/(\d+)/);
var page = matches[1];
var job = matches[2];