sammyjs optional parameters

后端 未结 4 1424
-上瘾入骨i
-上瘾入骨i 2021-01-02 23:27

Just wondering if there is anyway to specify a parameter as optional in a sammy js route.

I\'ve seen somewhere that you can use

route/:foo/?:bar
         


        
4条回答
  •  执念已碎
    2021-01-02 23:36

    Sammy actually dropped the ball when it comes to optional parameters and querystrings. The only way I could get this to work fairly well is to use regular expressions and the splat object. In your example, you would write :

    this.get(/\#\/route\/(.*)\/(.*)/, function (context) {
            var result = this.params['splat'];
    });
    

    The downside is that you need the backslash at the end of the URL when the optional parameter is omitted.

    The splat object is the actual result of the JavaScript match method and is an array.

    '#/route/test/' => {result[0]: 'test', result[1]: ''}
    '#/route/test/chicken' => {result[0]: 'test', result[1]: 'chicken'}
    

提交回复
热议问题