-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Child States are not waiting for parent state promise resolves #1903
Comments
The child states need the parent state as a dependency in order for it to wait to load, even if its not used directly. |
Perhaps this belongs in the FAQ? |
Can you give an example of how a child state includes a parent state as a dependency? I cannot inject the parent state or the parent state's resolver into the child state. |
angular.module 'example'.config ( $stateProvider ) ->
$stateProvider.state 'parent',
resolve: User: ( UserModel ) ->
UserModel.query().$promise
...
$stateProvider.state 'parent.child',
resolve: Profile: ( User ) ->
Profile.get( User.id ).$promise
|
+1 |
+1. Thanks so much @eddiemonge |
+1. I'm having the same issue. |
@bmkrocks1 which is what? Did you do like I showed in the example? |
@eddiemonge , I am having a problem which is similar to this. My child state shows a complete blank page, except the navigation bar, when it is refreshed via the browser. My parent state uses a |
+1 |
We are having this same issue with our app. The app was working with the solution @eddiemonge mentioned above. I wrote it off as a problem due to Angular 1.5 Beta, but it could be because of a newer version of ui-router (we are on 0.2.15) |
without code or examples its hard to troubleshoot the problems everyone else is having |
Fortunately for ui-router, I got some code review on my app. There had been a change to the promise chain and this caused the error. I was not returning the invocation of a factory function. See:
The problem was occurring because i did not have a return in front of the BookmarksFactory.load() promise: |
I'm also having an issue with this with the following case:
And I get the following error:
Now, I'm not sure if I'm misinterpreting the documentation but from what I've read, the child inherits the resolve from the parent, but in this case, it seems the child is resolving before the parent and thus not being able to display the page because it can't find the dependencies in the parent. |
Like I said in my first comment, you have to require the parent in the child. |
Oh, don't know how I missed that, it's working now, thanks! |
@adrianlungu how did you solve your problem? I can't understand anything happening in the snippet provided by eddiemonge, but your code is a lot more familiar and your solution could help me. Thanks. |
@deleugpn this is the code I ended up with:
The difference is very subtle, but basically it all comes down to these 2 lines: Basically, named the parent dependency array "depsAdmin" and gave it as a parameter to the child dependency "depsAdminSuper". Hope that helps! |
Is there a reason why the child state does not wait for the parent's resolves? It seems to me this should be the default behaviour. |
Seems like it works as expected in v.1.
Should have been a part of documentation for 0.2. |
Thanks @amalitsky, it worked!
Using ui-router 0.2.15. |
0.x legacy behaviorIn 0.x, all resolves are "eager". If you transition from state If there is an implicit dependency between parent and child resolves, the child resolve should inject the parent resolve, so it will wait for the parent to complete before the child starts. 1.0 behaviorIn 1.0, we added different resolve policies of In 1.0, resolves by default are "LAZY". A state's LAZY resolves will wait for the parent state's resolves to finish before beginning to load (in reality, LAZY resolves begin when the state is being entered) We made this change due to feedback like yours. Each state's resolves waiting for the parent states' seems the least surprising. If anyone wants "EAGER" behavior they can opt in. |
It's good to see a project evolve with user feedback. Thank you for the response @christopherthielen. |
Been chasing my tail on this, and I'm glad to find an explanation (lazy/eager), but the response by @christopherthielen leaves me a bit confused. He says for 0.x, "If there is an implicit dependency between parent and child resolves, the child resolve should inject the parent resolve, so it will wait for the parent to complete before the child starts." It seems that if a state is a child of another, that in itself is implicit, and that explicit would be if I specifically typed in things to inject. Also it took me a bit to figure out what was meant by eager and lazy, which I now understand to mean async (just run them and I don't care what order they come back or when) and sync (chained in order as proper dependencies and don't do anything else till they're done). The html script tag "async" attribute behaves this way. Semantics aside, using 0.x requires me to add a dummy resolver with parent dependencies for every child state, which is not "dry", and exactly what I was trying to avoid by using child states to begin with ("globally" waiting for firebase to initialize and checking login status before deciding if a user has privileges to go where they were trying to, in both "hot" and "cold" situations). I'm pasting these dummies on everything and getting odd results. Switching versions mid-project isn't an option. Is there a magic config I can toggle? |
No magic config, although my team is creating something which is "magically" wrapping child states with this logic to emulate what we think should be expected behavior. |
to clarify,
No, in 0.x all the resolves are processed first (eagerly). When they are done, then states are entered. in 0.x, resolves are processed as soon as possible (eagerly), when all of the resolve's dependencies are ready. Adding a dummy resolver with parent dependencies to a state has no effect on the state being entered. However, adding dummy parent dependencies to a resolve (such as a firebase request which requires auth) would have an effect. In 0.x, the only way to implement what you are attempting is with a state change start event (cancel the transition, ensure the user is authenticated, then re-run the transition), or via deferIntercept to when the application is first started. The problems with doing these things is precisely why ui-router 1.0 abandoned the state change events in favor of Transition Hooks, which allow you to implement your use case easily. |
If you have multiple promises being resolved in the child state, only those depending on the parent's promise will wait for it to be resolved. I had problems making it work, so I hope this comment will save time for someone else. |
Hi Guys,
According to the documentation child states should be able to access parent state resolved promises. In the documentation, https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views#inherited-resolved-dependencies "To provide resolved dependencies via resolve for use by child states." However it seems like when I check the property exists on state change start method, parent state promise has not been resolved.
I have created a small app based on http://scotch.io sample application. As you can see in authorize method I am checking user object exists in child state. I am setting the property in parent state. But property has not been set. Please see the sample app in http://plnkr.co/edit/hS8jKONk0ZfyJfE4r7Ty?p=preview
The text was updated successfully, but these errors were encountered: