Skip to content

Commit

Permalink
feat(Transition): Allow injector() to retrieve resolves for the exi…
Browse files Browse the repository at this point in the history
…ting states/path
  • Loading branch information
christopherthielen committed Jan 7, 2017
1 parent ad9ae81 commit df502e8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
46 changes: 44 additions & 2 deletions src/transition/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,55 @@ export class Transition implements IHookRegistry {
*
* The `UIInjector` can also provide values from the native root/global injector (ng1/ng2).
*
* #### Example:
* ```js
* .onEnter({ entering: 'myState' }, trans => {
* var myResolveValue = trans.injector().get('myResolve');
* // Inject a global service from the global/native injector (if it exists)
* var MyService = trans.injector().get('MyService');
* })
* ```
*
* In some cases (such as `onBefore`), you may need access to some resolve data but it has not yet been fetched.
* You can use [[UIInjector.getAsync]] to get a promise for the data.
* #### Example:
* ```js
* .onBefore({}, trans => {
* return trans.injector().getAsync('myResolve').then(myResolveValue =>
* return myResolveValue !== 'ABORT';
* });
* });
* ```
*
* If a `state` is provided, the injector that is returned will be limited to resolve values that the provided state has access to.
* This can be useful if both a parent state `foo` and a child state `foo.bar` have both defined a resolve such as `data`.
* #### Example:
* ```js
* .onEnter({ to: 'foo.bar' }, trans => {
* // returns result of `foo` state's `data` resolve
* // even though `foo.bar` also has a `data` resolve
* var fooData = trans.injector('foo').get('data');
* });
* ```
*
* If you need resolve data from the exiting states, pass `'from'` as `pathName`.
* The resolve data from the `from` path will be returned.
* #### Example:
* ```js
* .onExit({ exiting: 'foo.bar' }, trans => {
* // Gets the resolve value of `data` from the exiting state.
* var fooData = trans.injector(null, 'foo.bar').get('data');
* });
* ```
*
*
* @param state Limits the resolves provided to only the resolves the provided state has access to.
* @param pathName Default: `'to'`: Chooses the path for which to create the injector. Use this to access resolves for `exiting` states.
*
* @returns a [[UIInjector]]
*/
injector(state?: StateOrName): UIInjector {
let path: PathNode[] = this._treeChanges.to;
injector(state?: StateOrName, pathName = "to"): UIInjector {
let path: PathNode[] = this._treeChanges[pathName];
if (state) path = PathFactory.subPath(path, node => node.state === state || node.state.name === state);
return new ResolveContext(path).injector();
}
Expand Down
4 changes: 2 additions & 2 deletions test/stateServiceSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ describe('stateService', function () {
expect(trans.from().name).toBe('design');
expect(trans.to().name).toBe('A');
expect(state.self).toBe($registry.get('design'));
expect(trans.getResolveValue('cc', 'from')).toBe('cc resolve');
expect(trans.injector(null, 'from').get('cc')).toBe('cc resolve');
}
});

Expand Down Expand Up @@ -659,7 +659,7 @@ describe('stateService', function () {
}
},
onEnter: function ($transition$) {
let stateInfo = $transition$.getResolveValue('stateInfo');
let stateInfo = $transition$.injector().get('stateInfo');
log = stateInfo.join(' => ');
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/transitionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('transition', function () {
resolve: { cc: () => 'cc resolve' },
onExit: (trans, state) => {
expect(state.self).toBe(router.stateRegistry.get('design'));
expect(trans.getResolveValue('cc', 'from')).toBe('cc resolve');
expect(trans.injector(null, 'from').get('cc')).toBe('cc resolve');
done();
}
});
Expand Down

0 comments on commit df502e8

Please sign in to comment.