javascript 具有多个参数的骨干路由器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19977666/
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 router with multiple parameters
提问by matteok
I need to get this to work:
我需要让它工作:
routes: {
':product' : 'showProduct',
':product/:detail': 'showProductDetail'
showProductDetail never gets called while the ':product' route is set even if it is set afterwards. I tried the following
showProductDetail 永远不会在 ':product' 路由被设置时被调用,即使它是在之后设置的。我尝试了以下
routes: {
':product(/:detail)': showProductOrDetail
}
But this will not get called when only the second parameter changes. It is important that I have the product itself or the product and detail in the url.
但是当只有第二个参数改变时,这不会被调用。重要的是我在 url 中有产品本身或产品和详细信息。
Does anyone know how to fix this?
有谁知道如何解决这一问题?
回答by Szymon Drosdzol
There's a little hacky solution to your problem. I have a feeling there is a nicer way to do this but that should work:
有一个小技巧可以解决您的问题。我觉得有一种更好的方法可以做到这一点,但应该可行:
routes: {
"product/:id": "showProduct",
"product/:id/details/:did": "showDetails"
},
showProduct: function(id) {
this.showDetails(id);
},
showDetails: function(id, did) {
// Check did for undefined
}
回答by jstleger0
A late response (over a year).. but you can use RegEx in a backbone router to achieve this. My example presumes the parameters are going to start with a number.
迟到的响应(超过一年)。但您可以在主干路由器中使用 RegEx 来实现这一点。我的示例假定参数将以数字开头。
ie: localhost:8888/#root/1param/2param
即:本地主机:8888/#root/1param/2param
var router = Backbone.Router.extend({
initialize: function () {
// Use REGEX to get multiple parameters
this.route(/root/, 'page0');
this.route(/root\/(\d+\S+)/, 'page1');
this.route(/root\/(\d+\S+)\/(\d+\S+)/, 'page2');
},
page0:function(){
console.log("no id");
},
page1:function(id1){
console.log(id1);
},
page2:function(id1,id2){
console.log(id1);
console.log(id2);
}
});
Hope this helps.
希望这可以帮助。