1
我想用骨干路由设置的作品如下:Backbone.js路线中的变量:是否需要正则表达式?
- 路线像
/country/[any string]
,其中country
要么us
或ca
,都应该去handleByCountry(country, location)
- 任何其他途径应该重定向到
/
并由handleHome
处理。
目前,我有(作为粗第一次尝试):
var AppRouter = Backbone.Router.extend({
routes: {
"ca/:str": "handleByCountry",
"us/:str": "handleByCountry",
"*route": "home"
},
handleByCountry: function(str){
console.log('ROUTE handleByCountry', route);
// how to get the country? parse route by hand?
},
home: function(route){
this.navigate('/');
},
});
这有以下问题:
- 也不干 - 我可以写两个
ca
和匹配的单一路线us
?我需要某种条件变量,也许是一个正则表达式。 - 如何从内部
handleByCountry
检查当前的国家?我是否需要再次解析路由字符串?
此外,正在转发其他路线/
,因为我在做,好的做法?
接近,但也不能令人信服,至少对我来说...我需要限制'country'要么CA或美国,但没有别的。这是一个正则表达式的工作吗? – Richard 2012-02-22 16:39:46
是的,你需要使用'route'方法在你的路由器的初始化和创建一个自定义路线有使用正则表达式 - 检查它的文档http://backbonejs.org/#Router-route有使用正则表达式的例子那里 – 2012-02-22 21:32:17