Skip to content

Conversation

@brandonroberts
Copy link
Contributor

This provides public access to the current instruction and exposes the root router on each child router.

cc: @btford @petebacondarwin

@petebacondarwin
Copy link
Contributor

This is a reasonable idea. What do you think @btford ?

@brandonroberts
Copy link
Contributor Author

Finally got a green build on this one

@brandonroberts brandonroberts force-pushed the router-current-instruction branch 2 times, most recently from 525cf21 to 8e33163 Compare February 25, 2016 02:48
@petebacondarwin
Copy link
Contributor

I would like to think through the various use cases for accessing the complete instruction/URL/route information before agreeing to merge this...

@brandonroberts
Copy link
Contributor Author

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 router.recognize promise to turn that url into the instruction.

@brandonroberts
Copy link
Contributor Author

Another use case is reloading the current route programmatically. If you try to use router.renavigate, it doesn't work unless you used router.navigateByUrl previously. If that's the intended behavior, you could use router.navigateByInstruction(router.currentInstruction()) to reload the current route.

@petebacondarwin
Copy link
Contributor

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 renavigate() method is supposed to be used when the route configuration may have changed and that the URL may no longer correspond to the current instruction. So I don't think that you would use it for arbitrary reloading of routes.

I am not sure what the real world use case for reloading the current instruction is... Can you give one?

@brandonroberts
Copy link
Contributor Author

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

@petebacondarwin
Copy link
Contributor

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.

@brandonroberts
Copy link
Contributor Author

Yes

@petebacondarwin
Copy link
Contributor

OK, so there are two goals:

  1. support breadcrumb generation.
  2. support forcing a reload of the current instruction.

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 router.root.currentInstruction. What do you think @brandonroberts ?

The second is not even fixable at the moment by this PR, since calling navigateByInstruction will just reuse the current components unless they explicitly prevent reuse by implementing $canReuse and so they may not automatically update their state... Again what do you think @brandonroberts ?

@brandonroberts
Copy link
Contributor Author

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 router.parent to get to the root. Would you consider making the RootRouter injectable in route components like you did with ng1 component router? I don't know if that would be confusing to be able to inject the components router and the root router, but they serve different purposes.

Agreed on the second issue as well.

@brandonroberts brandonroberts force-pushed the router-current-instruction branch 4 times, most recently from fc21dc3 to 84ab16c Compare March 1, 2016 15:57
@brandonroberts
Copy link
Contributor Author

@petebacondarwin I updated this PR to include the changes we discussed to access the root router and make the current instruction public.

@MonkeyDo
Copy link

MonkeyDo commented Mar 1, 2016

Thanks @brandonroberts, that looks nice and easy.

@petebacondarwin
Copy link
Contributor

I'll take a look later today!!

@brandonroberts brandonroberts changed the title feat(router): Added method to get current instruction feat(router): Added public access to current instruction Mar 1, 2016
Copy link
Contributor

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;
  }
}

@petebacondarwin
Copy link
Contributor

One small note but I think the general idea is good.
I think we would need @btford or another Googler to chime in before we go ahead and modify the public API.

@brandonroberts
Copy link
Contributor Author

@petebacondarwin good idea. I made the change you suggested.

Copy link
Contributor

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.

Copy link
Contributor Author

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?

@brandonroberts brandonroberts force-pushed the router-current-instruction branch from 597deba to d98cdf4 Compare March 2, 2016 14:45
This method delegates to the root router to get the current complete instruction.
@brandonroberts brandonroberts force-pushed the router-current-instruction branch from d98cdf4 to ce596cf Compare March 2, 2016 14:58
@petebacondarwin
Copy link
Contributor

Great. Let's get @IgorMinar or @mhevery to review the API change.

@mhevery
Copy link
Contributor

mhevery commented Mar 2, 2016

API change is fine.

@brandonroberts
Copy link
Contributor Author

Cool. I got an error on the build but it wasn't caused by my changes.

@petebacondarwin
Copy link
Contributor

It was a flake! I reran the build and it is green now.

@petebacondarwin petebacondarwin added action: merge The PR is ready for merge by the caretaker and removed pr_state: LGTM labels Mar 2, 2016
@brandonroberts
Copy link
Contributor Author

Thanks!

@vikerman
Copy link
Contributor

vikerman commented Mar 3, 2016

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

@vikerman
Copy link
Contributor

vikerman commented Mar 4, 2016

TAP Green

@vikerman
Copy link
Contributor

vikerman commented Mar 4, 2016

Manually merged to master.

@vikerman vikerman closed this Mar 4, 2016
@brandonroberts brandonroberts deleted the router-current-instruction branch March 4, 2016 14:55
@angular-automatic-lock-bot
Copy link

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 Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker cla: yes effort1: hours feature Issue that requests a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants