-
Notifications
You must be signed in to change notification settings - Fork 26.7k
feat(router): Added public access to current instruction #7163
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
feat(router): Added public access to current instruction #7163
Conversation
|
This is a reasonable idea. What do you think @btford ? |
1047c65 to
c1d499d
Compare
|
Finally got a green build on this one |
525cf21 to
8e33163
Compare
|
I would like to think through the various use cases for accessing the complete instruction/URL/route information before agreeing to merge this... |
|
One use case is building a breadcrumb component. There isn't an easier way to get the instruction tree from a top down approach without accessing private properties. One way to get the full instruction is to subscribe to the router, get the url and resolve |
|
Another use case is reloading the current route programmatically. If you try to use |
|
I don't believe that we need this change for the use cases defined in #5335 But I accept @brandonroberts' first use case for building a breadcrumb tree. But I wonder if we need to make an API distinction between the "relative" current instruction, which is relative to the current router, and the "absolute" current instruction, which is relative to the root router? In the second use case, I believe that the I am not sure what the real world use case for reloading the current instruction is... Can you give one? |
|
@petebacondarwin I think a relative current instruction could be useful. My issue is that the router isn't going to force reload its parent routes and unless told not to, it will use reuse them. We already have the current instruction that are provided with the lifecycle hooks. Granted you can't use those for navigation and you can't use them to walk back up the route tree, but they are there. One use case would be to force reload the entire ui for the current route after logging in through a modal. A user goes to a page that is publicly accessible but only shows limited information until the user logs in. The user triggers an action to login, a modal is displayed, they login and the entire route which is composed of parent/child routes is refreshed. Maybe there needs to be a way force a reload instead? |
|
So what you are saying is that the resolution of a particular URL/route-instruction could be dependent upon information that is not contained in the URL/route-instruction such as whether the user is logged in. |
|
Yes |
|
OK, so there are two goals:
The first is fixed by this PR - by accessing the top level instruction, which would allow the developer to traverse down the instructions - but I think we should call the method something slightly different to emphasize that it is getting the complete instruction from the root. I was considering if we should expose the rootRouter directly on each router, then just expose the currentInstruction publicly. To get the full instruction we could then do The second is not even fixable at the moment by this PR, since calling |
|
With the first one, I'm fine with calling the method something different and exposing the current instruction. I think what you have proposed would be easier than traversing up the Agreed on the second issue as well. |
fc21dc3 to
84ab16c
Compare
|
@petebacondarwin I updated this PR to include the changes we discussed to access the root router and make the current instruction public. |
|
Thanks @brandonroberts, that looks nice and easy. |
|
I'll take a look later today!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this has to be generated here, right?
Can't we just have a straight attribute which is assigned in the constructor?
class RootRouter {
constructor() {
this.root = this;
}
}
class ChildRouter {
constructor(parent) {
this.root = parent.root;
}
}
|
One small note but I think the general idea is good. |
84ab16c to
597deba
Compare
|
@petebacondarwin good idea. I made the change you suggested. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to be a pain but we can make providing the root even more explicit by including it as a constructor parameter here:
constructor(public registry: RouteRegistry, public parent: Router, public root: Router, public hostComponent: any) {}Then we can ditch the declaration further up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. I moved the root property to the end of the constructor and made it optional to satisfy TypeScript. The ROUTER_PROVIDERS provides the root router, and it won't have a root.
constructor(public registry: RouteRegistry, public parent: Router, public hostComponent: any,
public root?: Router) {}
Is that ok?
597deba to
d98cdf4
Compare
This method delegates to the root router to get the current complete instruction.
d98cdf4 to
ce596cf
Compare
|
Great. Let's get @IgorMinar or @mhevery to review the API change. |
|
API change is fine. |
|
Cool. I got an error on the build but it wasn't caused by my changes. |
|
It was a flake! I reran the build and it is green now. |
|
Thanks! |
|
@petebacondarwin - Is this PR LGTM to be merged? FYI - We look for both pr_state:LGTM and pr_action: merge to look for PRs to merge. |
|
TAP Green |
|
Manually merged to master. |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This provides public access to the current instruction and exposes the root router on each child router.
cc: @btford @petebacondarwin