Skip to content

feat(router) added method to convert an Url to RouterStateSnapshot#15826

Closed
smurfy wants to merge 1 commit intoangular:masterfrom
smurfy:fix-15750
Closed

feat(router) added method to convert an Url to RouterStateSnapshot#15826
smurfy wants to merge 1 commit intoangular:masterfrom
smurfy:fix-15750

Conversation

@smurfy
Copy link
Copy Markdown

@smurfy smurfy commented Apr 7, 2017

Please check if the PR fulfills these requirements

What kind of change does this PR introduce? (check one with "x")

[ ] Bugfix
[x Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior? (You can also link to an open issue here)

There is no clean way to convert an url to a route.
See #15750

What is the new behavior?

You now can convert any url to a routestatesnapshot (if they exist).
This is will allows you to easily generate breadcrumbs or create a directive to check access to a routeLink

Does this PR introduce a breaking change? (check one with "x")

[ ] Yes
[x] No

Other information:

I did not include any tests because of the size of the change and the actual logic is in the recognize function which is already has its own tests.

@googlebot
Copy link
Copy Markdown

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@smurfy
Copy link
Copy Markdown
Author

smurfy commented Apr 7, 2017

I signed it!

@googlebot
Copy link
Copy Markdown

CLAs look good, thanks!

@googlebot googlebot added cla: yes and removed cla: no labels Apr 7, 2017
@smurfy smurfy force-pushed the fix-15750 branch 2 times, most recently from 268b80d to 7d1eb7d Compare April 10, 2017 08:11
@vicb vicb added area: router feature Label used to distinguish feature request from other issues labels Apr 10, 2017
@bijanajamlou
Copy link
Copy Markdown

What is the status of this? When can we expect this feature to be released?

@tartexs
Copy link
Copy Markdown

tartexs commented Jun 6, 2017

what's the status of this? any advances? I really need something like this

Copy link
Copy Markdown
Contributor

@jasonaden jasonaden left a comment

Choose a reason for hiding this comment

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

Thanks for the PR and sorry for the delay. This looks very useful.

One change requested, and please add a test for this method.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Our Observables do not have the Rxjs prototype methods. So you would need to import toPromise in order to return a promise here. So:

import {toPromise} from 'rxjs/operator/toPromise';

...

return toPromise.call(snapshot$);

@jasonaden jasonaden self-assigned this Jul 13, 2017
@jasonaden jasonaden added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jul 13, 2017
@jasonaden
Copy link
Copy Markdown
Contributor

@smurfy Are you able to make the updates on this PR?

@smurfy
Copy link
Copy Markdown
Author

smurfy commented Jul 26, 2017

@jasonaden I will have a look tomorrow regarding the promise thing.

Regarding Unit tests i need to read-up on. We only do selenium tests and as mentioned in the initial comment, the new method is only a wrapper for "recognize" which is already unit tested quite heavily. So i'm not sure if we really need unit tests.

@smurfy
Copy link
Copy Markdown
Author

smurfy commented Jul 27, 2017

I updated my PR with your suggested changes and added an unit-test.
I also did a test-build and everything still works with 4.3.1

@smurfy smurfy force-pushed the fix-15750 branch 3 times, most recently from c8d2d13 to 6ea35fa Compare August 7, 2017 16:06
@smurfy
Copy link
Copy Markdown
Author

smurfy commented Aug 7, 2017

@jasonaden Updated the code to be conflict free to master, also fixed tslint errors, which was quite stupid, because somehow a local tslint does not show the errors.

@mitchellwills
Copy link
Copy Markdown
Contributor

Any updates here? I'd love to see this PR land

@jasonaden
Copy link
Copy Markdown
Contributor

I took another look at this PR and have some different thoughts on it.

Right now you're doing applyRedirects which is an async operation. But in the case of a URL to RouterStateSnapshot, I think we would want that to be sync. If you were to try to call the method with a URL where the RouterStateSnapshot doesn't exist, we would simply return null.

I can't think of a use case where we actually want this method to be async. If you can think of one, let me know. Otherwise we should convert this to be sync instead.

@smurfy
Copy link
Copy Markdown
Author

smurfy commented Dec 22, 2017

@jasonaden well i think async makes sense, because it is possible, that not all routes are available yet. due to async modules.

Other than that, the main logic needed is in the recognize function anyways. I added the applyRedirects call, so all internal works are similiar to a router.navigate but instead of really navigating to the resolved RouterStateSnapshot it returns it.

@ashwinirajput
Copy link
Copy Markdown

@jasonaden how you are using applyRedirects

@bioub
Copy link
Copy Markdown

bioub commented Jan 11, 2018

Please note that this wouldn't work for Lazy Loaded modules...

@smurfy
Copy link
Copy Markdown
Author

smurfy commented Jan 11, 2018

Please note that this wouldn't work for Lazy Loaded modules...

well it does not trigger the loading of all modules to get all routes, but if the module is already loaded it works.

@bevrard
Copy link
Copy Markdown

bevrard commented Mar 1, 2018

Any news on this?

@mleibman
Copy link
Copy Markdown

Ping. This would be very useful indeed.

@killianhale-work
Copy link
Copy Markdown

@smurfy - This would be very useful indeed and is exactly what I'm looking for. Can you update this to the latest version of angular?
@jasonaden - Looks like the git history was rewritten but that your initial change requests were made. Care to update your response?

Thanks!

@yelhouti
Copy link
Copy Markdown
Contributor

yelhouti commented Oct 2, 2018

I really need this two...

@yelhouti
Copy link
Copy Markdown
Contributor

yelhouti commented Oct 2, 2018

Hey @jasonaden , I'm doing this as soon as I have your answer.
Do you want me to modify recognize and make it return a RouterStateSnapshot then export it through the router, which will involve modifying all the calls/tests...
Or just write an new function syncRecognize which has all the content of recognize, and make recognize call it and don't touch the other code (calls/tests)?
Thanks in advance

@jasonaden
Copy link
Copy Markdown
Contributor

We're trying to clean up the oldest PRs now, and this is the oldest Router PR (sorry for all the delays).

Overall it looks good. There have been a number of changes to router.ts and this should be rebased. You should also use .pipe instead of the old .call syntax we were using when you wrote this.

Other than that, I believe this is good to go.

@smurfy
Copy link
Copy Markdown
Author

smurfy commented Oct 10, 2018

Ok, try to rebase it today

@jasonaden
Copy link
Copy Markdown
Contributor

@smurfy Let me know if you need help on the rebase here. I'm sometimes on Gitter, or you can DM me on Twitter at jasonaden1

]);

r.urlToRouteStateSnapshot('/a').then(
(s) => { expect(s.root.firstChild !.data).toEqual({state: 'i am A'}); });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, do these asynchronous expectations actually work reliably? I think nowadays we should use async/await instead, as doing so will return a Promise to jasmine which it will wait for.

Copy link
Copy Markdown
Contributor

@yelhouti yelhouti May 11, 2019

Choose a reason for hiding this comment

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

No they don't, tested it

@yelhouti
Copy link
Copy Markdown
Contributor

Any news on this please?

@yelhouti
Copy link
Copy Markdown
Contributor

@jasonaden since you declined the merge how can I help to fix this please so you accept it. I really need this feature. Thanks.

Copy link
Copy Markdown
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

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

Please also address the comments in #15826 (comment)

recognize(this.rootComponentType, this.config, appliedUrl, this.serializeUrl(appliedUrl)),
(snapshot: any) => { return snapshot; });
});
return toPromise.call(snapshot$);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not just leave it as an Observable and let the caller decide if they want to use toPromise or not?

path: 'a',
component: ComponentA,
data: {state: 'i am A'},
children: [{path: 'b', component: ComponentB, data: {state: 'i am B'}}]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's also add a test for lazy loaded modules. Even if it doesn't work (though I don't immediately see why it wouldn't), the test documents the behavior. It does seem to me like this would actually trigger the lazy loading. If that's true, this needs to be documented.

@atscott
Copy link
Copy Markdown
Contributor

atscott commented Jan 14, 2021

Closing due to lack of activity. If you're still interested in working on this change, please open a new PR (and address the feedback from this one) and I'll be happy to review it. Thanks!

@atscott atscott closed this Jan 14, 2021
@angular-automatic-lock-bot
Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews area: router cla: yes feature Label used to distinguish feature request from other issues freq2: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.