Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding "greedy" route parameters #18

Open
liuq opened this issue Oct 14, 2015 · 2 comments
Open

Adding "greedy" route parameters #18

liuq opened this issue Oct 14, 2015 · 2 comments

Comments

@liuq
Copy link

liuq commented Oct 14, 2015

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)) + "(:?/|$)";
@gabrielmoreira
Copy link
Owner

@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?

@liuq
Copy link
Author

liuq commented Oct 17, 2015

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants