Backbone router with multiple parameters

馋奶兔 提交于 2019-11-30 14:15:47

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

}

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.

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