Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat($route): express style route matching #1745

Closed

Conversation

joshrtay
Copy link
Contributor

visionmedia/express style route matching:

  • optional params
  • wildcard params
  • regexes
.when('/bar/:foo?)
  $location.path('/bar') --> $routePrams: {}
  $location.path('/bar/foovalue') --> $routePrams: {foo: 'foovalue'}

.when('/bar/*')
  $location.path('/bar/foovalue') --> $routeParams: {0: 'foovalue'}

.when('/bar/*.*)
  $location.path('/bar/foo.js') --> $routeParams: {0: 'foo', 1: 'js}

.when(/\/(\d+)/)
  $location.path('/12') --> $routeParams: {0: '12'}

@Keyamoon
Copy link
Contributor

If you want your pull requests to be accepted, you need to add tests, change your commit message and sign CLA as described here: http://docs.angularjs.org/misc/contribute

@joshrtay
Copy link
Contributor Author

The commit has tests and I signed the CLA yesterday.

@Keyamoon
Copy link
Contributor

You still need to change the commit message. Just letting you know. I had to go through the same process.

@joshrtay
Copy link
Contributor Author

what do you mean by change the commit message?

@Keyamoon
Copy link
Contributor

@joshrtay
Copy link
Contributor Author

Thanks for the help. Is there anything else I'm missing?

@Keyamoon
Copy link
Contributor

No problem. I think that's it, nothing else missing.

@joshrtay joshrtay closed this Jan 9, 2013
@xrado
Copy link

xrado commented Feb 24, 2013

this is the feature I'm waiting for, why you are not merge it in?

@IgorMinar IgorMinar reopened this Feb 25, 2013
@LucaLanziani
Copy link

+1 how long is needed to merge it?

* @methodOf ng$routeProvider
*
* @description
* Options fore route matching.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo "fore" => "for"

@petebacondarwin
Copy link
Contributor

  • Contributor signed CLA now or in the past (if you just signed, leave a comment here with your real name for cross reference)
  • Feature improves existing core functionality
  • API is compatible with existing Angular apis and relevant standards (if applicable)
  • PR doesn't contain a breaking change
  • PR contains unit tests
  • PR contains e2e tests (if suitable)
  • PR contains documentation update
  • PR passes all tests on Travis (sanity)
  • PR passes all tests on ci.angularjs.org (cross-browser compatibility)
  • PR is rebased against recent master
  • PR is squashed into one commit per logical change
  • PR's commit messages are descriptive and allows us to autogenerate release notes (required commit message format)
  • All changes requested in review have been implemented

@petebacondarwin
Copy link
Contributor

@joshrtay - this PR is now a bit outdated and it is difficult to rebase against the current master. Can you take a look at this please?

@joshrtay
Copy link
Contributor Author

joshrtay commented May 1, 2013

ya i'll update it.

@joshrtay
Copy link
Contributor Author

joshrtay commented May 1, 2013

it looks like the way you guys have implemented wildcards is incompatible with the way express does it. wildcards in express are unnamed. how would you like me to proceed?

@vojtajina
Copy link
Contributor

@joshrtay thanks for doing this.

Let's add only optional params for now and merge it in.

I think having named wildchars is more expressive so I would prefer keeping that. That means, you can't do stuff like /bar/*.*. I think, in the future, we could make . to be a separator in the same way / is, so you would be able to do stuff like:

.when('user/:id.:format')
// /user/10.json -> {id: '10', format: 'json'}

If we wanna get super crazy, we could later allow regexp matching as well:

.when('/user/:id(\d)'
// /user/23 -> {id: 23}
// /user/x would not match

But at that point, allowing a custom match function rather than a string would be probably better.

Guys @xrado @NSS do you have any other use case that is missing ?

@xrado
Copy link

xrado commented May 2, 2013

optional params are the only thing I miss

@joshrtay
Copy link
Contributor Author

joshrtay commented May 3, 2013

optional params working

@jayzeng
Copy link

jayzeng commented May 3, 2013

Long waited feature, definitely 👍


$location.path('/bar/foovalue');
$rootScope.$digest();
expect($routeParams).toEqual({foo: 'foovalue'});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test that even without the optional value the route still matches - currently you are only checking the routeParams I was thinking that we should check the current route to see that it has been matched correctly but I guess that this is implicit.

What about paths that don't just end in an optional parameter?

/bar/:foo?/edit -> will this match /bar/fooValue/edit and /bar/edit ?

  • We should have tests of these cases for both ? and * style optional parameters.

@joshrtay
Copy link
Contributor Author

joshrtay commented May 3, 2013

I'm not sure what you mean by the routes still match, but I added some more routeParams tests.

Also, my implementation supports this:

.when('user/:id.:format')
// /user/10.json -> {id: '10', format: 'json'}

I think that named wildcards should be considered very carefully, since it seems to go against wildcard convention. Most people would assume that /Book1/*highlight/edit matches
/Book1/Moby/Chapter/highlight/edit.

Unnamed wildcards, though not as expressive, are more consistent with how wildcards typically work and you can still access the values by position.

@petebacondarwin
Copy link
Contributor

@joshrtay & @vojtajina - I kind of agree that the named wildcard format is not so intuitive. I wonder if it would be clearer to use a similar convention to the optional parameter:

/Book1/:highlight*/edit

would match Book1/x/y/z/edit and then routeParams.highlight == 'x/y/z'

@petebacondarwin
Copy link
Contributor

In the meantime I think we are about there to merge this.

@petebacondarwin
Copy link
Contributor

@joshrtay - sorry but IE doesn't like your code: http://ci.angularjs.org/job/angular.js-pete/139/console :-(

@joshrtay
Copy link
Contributor Author

joshrtay commented May 7, 2013

@petebacondarwin - i dont have a great way of testing on IE. any thoughts on what is causing the errors?

@petebacondarwin
Copy link
Contributor

My thought was that these are not like unordered params so optionalParam2
could not be used without optionalParam1.

Pete
...from my mobile.
On 13 Jul 2013 18:27, "Igor Minar" notifications@github.com wrote:

@petebacondarwin https://github.com/petebacondarwin but that means that
for route /path/:optionalParam1?/:optionalParam2? and path /path//foo foowould become
optionalParam1. that's wrong.

Let's say that we start supporting creation of urls from route templates
(which we should), if you generate a link from the template above and hash {optionalParam2:
'foo'} the result should match the template above and result in
routeParams -> {optionalParam2: 'foo'}. If we did what you suggested,
routeParams would contain 'foo' key-ed off of optionalParam1. That's
wrong.


Reply to this email directly or view it on GitHubhttps://github.com//pull/1745#issuecomment-20923267
.

@joshrtay
Copy link
Contributor Author

@IgorMinar that proposal sounds good. I think we should allow /path//foo.

@IgorMinar
Copy link
Contributor

ok, I'm fine with /path//foo.

can we move forward and making the code change?

@joshrtay
Copy link
Contributor Author

i'll do it this weekend.

*
* For example, routes like `/color/:color/largecode/*largecode/edit` will match
* For example, routes like `/color/:color/largecode/:largecode*\/edit` will match
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to add \ so */ didn't end the comment. will this be a problem for the docs?

@joshrtay
Copy link
Contributor Author

code changes were made. let me know what you think.

Added new route matching capabilities:
  - optional param
Changed route matching syntax:
 - named wildcard
@joshrtay
Copy link
Contributor Author

sorry that last commit wasn't rebased against master

@joshrtay
Copy link
Contributor Author

joshrtay commented Aug 5, 2013

@IgorMinar waiting for some input on the changes ... would you like me to submit the syntax change to wildcard params as a new pull request?

@jbdeboer
Copy link
Contributor

jbdeboer commented Aug 9, 2013

I am seeing a few test failures on this PR.

  • $route events should NOT load cross domain templates by default
  • ngView animations should not double compile when the route changes

Investigating further.

  • Rebasing this CL on-top of master fixes the failures. Yay.

@joshrtay
Copy link
Contributor Author

joshrtay commented Aug 9, 2013

where are you seeing those test failures?

as far as I can tell, all the tests are passing.

@jbdeboer
Copy link
Contributor

jbdeboer commented Aug 9, 2013

All tests are passing for me when I rebase this CL; it could have been my environment.

*
* For example, routes like `/color/:color/largecode/*largecode/edit` will match
* For example, routes like `/color/:color/largecode/:largecode*\/edit` will match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the backslash before /edit belong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i want it to read /color/:color/largecode/:largecode*/edit, but the */ ends the comment. what should i do?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! I missed that.

@jbdeboer
Copy link
Contributor

I added a few more test and fixed the style comments in https://github.com/jbdeboer/angular.js/tree/pr1745

Looks good to merge and I will do the merge shortly.

@jbdeboer
Copy link
Contributor

Hmm; taking another look at this PR: @IgorMinar's spec said "an optional param cannot be be followed by a string literal, required or wildcard param"

There doesn't seem to be any code that enforced that requirement. The tests check for this case explicitly: '/test/:opt?/:baz/edit

Should we add an explicit check for trailing parameters after an optional?

@joshrtay
Copy link
Contributor Author

Is that something we really want to enforce? It seems ugly, but it is
valid.

On Friday, August 9, 2013, James deBoer wrote:

Hmm; taking another look at this PR: @IgorMinarhttps://github.com/IgorMinar's
spec said "an optional param cannot be be followed by a string literal,
required or wildcard param"

There doesn't seem to be any code that enforced that requirement. The
tests check for this case explicitly: '/test/:opt?/:baz/edit

Should we add an explicit check for trailing parameters after an optional?


Reply to this email directly or view it on GitHubhttps://github.com//pull/1745#issuecomment-22432080
.

@jbdeboer
Copy link
Contributor

I would like to hear from @IgorMinar, @petebacondarwin or @vojtajina who were involved in the discussion about literals or required parameters after an optional parameter and have more context.

Re-reading the discussion, the justification for optional parameters seems thin. I see it is a convenience in some situations. However, using optional parameters moves routing decisions out of the route declaration and into controllers (e.g. I can imagine code that checks for an empty optional parameter and then loads a completely different view).

By banning required parameters and string literals after the optional parameters, we would be purposely limited the power, and thus the abuse, of optional parameters.

On the other hand, there is no technical reason for such a ban. Getting this feature into the hands of developers would help us understand how they use it.

@jbdeboer
Copy link
Contributor

I talked to Igor and he is good with this implementation that allows literals and required parameters after optional parameters.

Landed as 04cebcc

@joshrtay Thank you so much for work through this multi-month PR!

@jbdeboer jbdeboer closed this Aug 12, 2013
@joshrtay
Copy link
Contributor Author

@jbdeboer no problem. glad to help.

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

Successfully merging this pull request may close these issues.

9 participants