Skip to content

A parent state's resolves are resolved twice when a child state's resolve are rejected on initial run #2641

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

Closed
Septh opened this issue Mar 23, 2016 · 3 comments
Labels
Milestone

Comments

@Septh
Copy link

Septh commented Mar 23, 2016

I came across this subtle bug while implementing two distinct "zones" in my app -- namely a "visitors only" zone and a "members only" zone. This is tested with Angular 1.5.1 and UI Router 0.2.18

Consider this scenario:

  • root state is an abstract state that dynamically loads (through a resolve) the current user's profile. This resolve is injected into direct child states to make sure they're not instanciated before the profile is finished loading
  • child state 1's own resolve checks if the user is registered. If so, the resolve is rejected and the user redirected to child state 2
  • child state 2's own resolve checks if the user is not registered. If so, the resolve is rejected and the user redirected to child state 1

So, in effect: child state 1 is my "visitors only" zone while child state 2 is my "members only" zone.

Now, assume I am a registered user. When I first launch the app, here's what happens:

  1. root state is resolved, my user profile is loaded
  2. child state 1's resolve is rejected (since I'm a member) and I am redirected to child state 2
  3. root state is resolved again! (and the API to load the profile is called again...)
  4. child state 2' is resolved and successfully activated

In my understanding, step 3 shouldn't happen -- root's already been resolved previously and should not be resolved gain. Or am I getting something wrong?

Note this only happens on the app's initial launch; if I try to navigate to state 1 (eg. by clicking an ui-sref), then only state 1's resolve is tested and correctly rejected without root state being resolved again.

I've made a Plunker to demonstrate: https://plnkr.co/edit/Nne19FArvUf0W4JHOtB7

@christopherthielen
Copy link
Contributor

Thanks for the plunker!

In my understanding, step 3 shouldn't happen -- root's already been resolved previously and should not be resolved gain. Or am I getting something wrong?

You are misunderstanding. The initial transition is cancelled and a brand new one started (when you do your check and redirect). Think of each transition as atomic; it either happens fully or not at all.

The 1.0 release is specifically addressing this use case. We support redirects from parent to child states while retaining previously resolved values.

I'll leave this ticket open and make sure it works before 1.0 final

@christopherthielen christopherthielen added this to the 1.0.0-final milestone Mar 26, 2016
@Septh
Copy link
Author

Septh commented Mar 28, 2016

You are misunderstanding. The initial transition is cancelled and a brand new one started (when you do your check and redirect). Think of each transition as atomic; it either happens fully or not at all.

Oh, I see. Well, I think that I can live with a flag in root's resolve to make sure the API isn't called twice ;)

The 1.0 release is specifically addressing this use case. We support redirects from parent to child states while retaining previously resolved values.

Can't wait to try the production-ready version, then.

@willhoney7
Copy link

Any updates to this? Just ran into this issue here with basically the same use case as @Septh.

@christopherthielen christopherthielen modified the milestones: 1.0.0-final, 1.0.0-beta.3 Sep 22, 2016
@christopherthielen christopherthielen modified the milestones: 1.0.0-beta.3, 1.0.0-final Sep 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants