Backbone.js的查询时传递到路由包含路由/ [英] backbone.js routing when query passed to route contains /
问题描述
我的应用程序基本上采取某种形式的输入,并返回一组结果。我有两个途径
My app basically takes some form input and returns a set of results. I have two routes
routes: {
'': 'search',
'search': 'search',
'results/:query': 'results'
},
results: function(query) {
var search = new ResultsSearchView();
var grid = new GridView({ query: query });
}
如果查询包含任何字符/专(完全可以在这种情况下发生的),它们被添加到URL和我的路线休息。
If the query contains any characters / specifically (can totally happen in this case), they are added to the URL and my route breaks.
我已经尝试使用连接codeURI()
和连接codeURIComponent()
位I M没有任何运气。你是如何处理的乡亲这样的事情?
I have tried using encodeURI()
and encodeURIComponent()
bit I'm not having any luck. How are you folks handling such things?
推荐答案
您可以使用<一个href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/en$c$cURIComponent\"相对=nofollow> 连接codeURIComponent
建设要转换的URL时, /
来%2F
,然后<一个href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/de$c$cURIComponent\"相对=nofollow> 德codeURIComponent
的路由处理程序将其转换回场内;在HTML将结束在寻找这样的:
You can use encodeURIComponent
when building the URL to convert the /
to %2F
and then decodeURIComponent
inside the route handler to convert them back; the HTML would end looking like this:
<a href="#results/pancakes">no slash</a>
<a href="#results/where%2Fis%2Fpancakes%2Fhouse">with slashes</a>
和然后在路由器:
routes: {
'results/:query': 'results'
},
results: function(query) {
query = decodeURIComponent(query);
// Do useful things here...
}
演示: http://jsfiddle.net/ambiguous/sbpfD/
另外,你可以使用图示路线:
路由可以包含参数部分,:参数
,匹配斜杠之间的单一URL分量;和图示部分 *图示
,它可以匹配任何数量的URL组件。
Routes can contain parameter parts,
:param
, which match a single URL component between slashes; and splat parts*splat
, which can match any number of URL components.
所以,你的HTML会是这样的:
So your HTML would be like this:
<a href="#results/pancakes">no slash</a>
<a href="#results/where/is/pancakes/house">with slashes</a>
和路由器:
routes: {
'results/*query': 'results'
},
results: function(query) {
// Do useful things here...
}
演示: http://jsfiddle.net/ambiguous/awJxG/
这篇关于Backbone.js的查询时传递到路由包含路由/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!