Skip to content

Commit df502e8

Browse files
feat(Transition): Allow injector() to retrieve resolves for the exiting states/path
1 parent ad9ae81 commit df502e8

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/transition/transition.ts

+44-2
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,55 @@ export class Transition implements IHookRegistry {
276276
*
277277
* The `UIInjector` can also provide values from the native root/global injector (ng1/ng2).
278278
*
279+
* #### Example:
280+
* ```js
281+
* .onEnter({ entering: 'myState' }, trans => {
282+
* var myResolveValue = trans.injector().get('myResolve');
283+
* // Inject a global service from the global/native injector (if it exists)
284+
* var MyService = trans.injector().get('MyService');
285+
* })
286+
* ```
287+
*
288+
* In some cases (such as `onBefore`), you may need access to some resolve data but it has not yet been fetched.
289+
* You can use [[UIInjector.getAsync]] to get a promise for the data.
290+
* #### Example:
291+
* ```js
292+
* .onBefore({}, trans => {
293+
* return trans.injector().getAsync('myResolve').then(myResolveValue =>
294+
* return myResolveValue !== 'ABORT';
295+
* });
296+
* });
297+
* ```
298+
*
279299
* If a `state` is provided, the injector that is returned will be limited to resolve values that the provided state has access to.
300+
* This can be useful if both a parent state `foo` and a child state `foo.bar` have both defined a resolve such as `data`.
301+
* #### Example:
302+
* ```js
303+
* .onEnter({ to: 'foo.bar' }, trans => {
304+
* // returns result of `foo` state's `data` resolve
305+
* // even though `foo.bar` also has a `data` resolve
306+
* var fooData = trans.injector('foo').get('data');
307+
* });
308+
* ```
309+
*
310+
* If you need resolve data from the exiting states, pass `'from'` as `pathName`.
311+
* The resolve data from the `from` path will be returned.
312+
* #### Example:
313+
* ```js
314+
* .onExit({ exiting: 'foo.bar' }, trans => {
315+
* // Gets the resolve value of `data` from the exiting state.
316+
* var fooData = trans.injector(null, 'foo.bar').get('data');
317+
* });
318+
* ```
319+
*
280320
*
281321
* @param state Limits the resolves provided to only the resolves the provided state has access to.
322+
* @param pathName Default: `'to'`: Chooses the path for which to create the injector. Use this to access resolves for `exiting` states.
323+
*
282324
* @returns a [[UIInjector]]
283325
*/
284-
injector(state?: StateOrName): UIInjector {
285-
let path: PathNode[] = this._treeChanges.to;
326+
injector(state?: StateOrName, pathName = "to"): UIInjector {
327+
let path: PathNode[] = this._treeChanges[pathName];
286328
if (state) path = PathFactory.subPath(path, node => node.state === state || node.state.name === state);
287329
return new ResolveContext(path).injector();
288330
}

test/stateServiceSpec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ describe('stateService', function () {
592592
expect(trans.from().name).toBe('design');
593593
expect(trans.to().name).toBe('A');
594594
expect(state.self).toBe($registry.get('design'));
595-
expect(trans.getResolveValue('cc', 'from')).toBe('cc resolve');
595+
expect(trans.injector(null, 'from').get('cc')).toBe('cc resolve');
596596
}
597597
});
598598

@@ -659,7 +659,7 @@ describe('stateService', function () {
659659
}
660660
},
661661
onEnter: function ($transition$) {
662-
let stateInfo = $transition$.getResolveValue('stateInfo');
662+
let stateInfo = $transition$.injector().get('stateInfo');
663663
log = stateInfo.join(' => ');
664664
}
665665
});

test/transitionSpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ describe('transition', function () {
404404
resolve: { cc: () => 'cc resolve' },
405405
onExit: (trans, state) => {
406406
expect(state.self).toBe(router.stateRegistry.get('design'));
407-
expect(trans.getResolveValue('cc', 'from')).toBe('cc resolve');
407+
expect(trans.injector(null, 'from').get('cc')).toBe('cc resolve');
408408
done();
409409
}
410410
});

0 commit comments

Comments
 (0)