austbot
austbot

Reputation: 75

ui-router State Children and Resolvers

I have a parent abstract state that has a resolver that gets data from the server.

Then by nature of ui-routers resolver inheritance I can access the resolved data in the child state controllers.

....controller('bla', function(resolvedData){....

Everything is working great, but I'm implementing a feature in the application that needs to change part of that resolved data and have it reflected across the app.

I'm assuming that the resolved data is not a reference to one object but it is a copy of what the service returned correct? Meaning, that if in the parent abstract controller I change a part of the resolved data it will NOT reflect in the child states since they would be looking at the original copy of the resolved data from the data service.

Upvotes: 1

Views: 85

Answers (2)

Radim Köhler
Radim Köhler

Reputation: 123861

I'm assuming that the resolved data is not a reference to one object but it is a copy of what the service returned correct?

NO. UI-Router passes what was returned. If it was a reference, it will be shared by all consumers.

Check the code (no cloning mentioned at all - just passing what was resolved) - state.js line 455

 /**

 * ....

 * @param {object=} stateConfig.resolve
 * <a id='resolve'></a>
 *
 * An optional map&lt;string, function&gt; of dependencies which
 *   should be injected into the controller. If any of these dependencies are promises, 
 *   the router will wait for them all to be resolved before the controller is instantiated.
 *   If all the promises are resolved successfully, the $stateChangeSuccess event is fired
 *   and the values of the resolved promises are injected into any controllers that reference them.
 *   If any  of the promises are rejected the $stateChangeError event is fired.

 * ... */

Upvotes: 1

austbot
austbot

Reputation: 75

Actually after testing it seems that the ui-router resolver does carry changes to the resolved data to child states, furthermore if I change the resolved data in a sibling state the changes are reflected within another sibling state.

I'm trying to find exactly where in the ui-router code it does this. https://github.com/angular-ui/ui-router/blob/master/src/resolve.js

Upvotes: 0

Related Questions