riot-router icon indicating copy to clipboard operation
riot-router copied to clipboard

Adding "greedy" route parameters

Open liuq opened this issue 9 years ago • 2 comments

Hi,

in a project of mine I made a small patch to the Route class, so that it can intercept also "greedy" parameters. That is, the last path parameter could be marked with an asterisk and captures all the remaining part of the path.

For example, the following route:

  new Route({ path: 'problem/:problemClass/*mySubPath', ... });

will match the routes problem/classA/x/y/w/z setting opts.problemClass to classA and opts.mySubPath to x/y/w/z.

If you find this useful, this snippet of code in the Route class constructor does the magic.

this.pattern = "^/?" + path.replace(/:([^/]+)/g, function(ignored, group) {
      this.pathParameterNames.push(group);
      return "([^/]+)";
    }.bind(this)).replace(/\*([^/]+)$/g, function(ignored, group) {
      this.pathParameterNames.push(group);
      return "(.+)$";
    }.bind(this)) + "(:?/|$)";

liuq avatar Oct 14 '15 15:10 liuq

@liuq, Thank you. It can be really usefull. I'm think about syntax alternatives before commiting.

your proposal:

new Route({ path: 'problem/:problemClass/*mySubPath', ... });

alternative:

new Route({ path: 'problem/:problemClass/:mySubPath*', ... });

or like page.js matching paths:

new Route({ path: 'problem/:problemClass/:mySubPath(*)', ... });

What do you think about it?

gabrielmoreira avatar Oct 17 '15 06:10 gabrielmoreira

@gabrielmoreira Hi Gabriel,

yes, I have to admit that my syntax was a bit cumbersome.

Both alternatives are ok, in my view, I found the "page.js" one is a bit verbose but I do not have a strong opinion against it.

liuq avatar Oct 17 '15 07:10 liuq