Backbone router with multiple parameters

本秂侑毒 提交于 2019-12-18 16:53:56

问题


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

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.

Does anyone know how to fix this?


回答1:


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

}



回答2:


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.

ie: localhost: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.



来源:https://stackoverflow.com/questions/19977666/backbone-router-with-multiple-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!