Backbone router with multiple parameters

前端 未结 2 559
不思量自难忘°
不思量自难忘° 2021-01-02 15:30

I need to get this to work:

routes: {
  \':product\' : \'showProduct\',
  \':product/:detail\': \'showProductDetail\'

showProductDetail nev

相关标签:
2条回答
  • 2021-01-02 16:02

    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
    
    }
    
    0 讨论(0)
  • 2021-01-02 16:14

    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.

    0 讨论(0)
提交回复
热议问题