带regex和extend的主干路由器
在谷歌搜索时,我看到很多这样的例子:带regex和extend的主干路由器,regex,backbone.js,backbone-routing,Regex,Backbone.js,Backbone Routing,在谷歌搜索时,我看到很多这样的例子: App.Router = Backbone.Router.extend({ routes: { '': 'index', 'show/:id': 'show' }, index: function(){ $(document.body).append("Index route has been called.."); }, show: function(id){ $(document.bo
App.Router = Backbone.Router.extend({
routes: {
'': 'index',
'show/:id': 'show'
},
index: function(){
$(document.body).append("Index route has been called..");
},
show: function(id){
$(document.body).append("Show route with id: " id);
}
});
App.Router = Backbone.Router.extend({
initialize: function() {
this.route(/^$/, 'index');
this.route(/^show\/(\d+)$/,"show");
this.route(/^show\/([A-Za-z]+)/, "showSpecial");
}
})
这个实现在使用regex时会是什么样子
我想要像这样的东西:
App.Router = Backbone.Router.extend({
routes: {
'': 'index',
/show/(\d+:id)/: 'show'
/show/([A-Za-z]+:other)/: 'showSpecial'
},
其中第一个正则表达式与/show/[anynumber]
匹配,并将id
参数中的数字传递给show
函数
第二个正则表达式与
/show/[any word]
匹配,并将other
参数中的单词传递给showSpecial
函数。我认为这种语法不起作用:
App.Router = Backbone.Router.extend({
routes: {
'': 'index',
/show/(\d+:id)/: 'show'
/show/([A-Za-z]+:other)/: 'showSpecial'
},
你可以这样写:
App.Router = Backbone.Router.extend({
routes: {
'': 'index',
'show/:id': 'show'
},
index: function(){
$(document.body).append("Index route has been called..");
},
show: function(id){
$(document.body).append("Show route with id: " id);
}
});
App.Router = Backbone.Router.extend({
initialize: function() {
this.route(/^$/, 'index');
this.route(/^show\/(\d+)$/,"show");
this.route(/^show\/([A-Za-z]+)/, "showSpecial");
}
})
检查这个