Skip to content

Commit aa37ee7

Browse files
docs(TransitionHook): Document the built-in transition hooks.
1 parent 421bca7 commit aa37ee7

14 files changed

+180
-90
lines changed

src/common/hof.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Higher order functions
33
*
44
* @module common_hof
5-
*/
5+
*/ /** */
66

77
import {Predicate} from "./common";
88
/**

src/common/interface.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @module common */ /** */
2+
13
/**
24
* An interface for getting values from dependency injection.
35
*/
@@ -47,4 +49,4 @@ export interface UiInjector {
4749
* @return a Promise for the Dependency Injection value that matches the key
4850
*/
4951
getAsync(key: any): any;
50-
}
52+
}

src/hooks/onEnterExitRetain.ts

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
1-
/** @module state */ /** for typedoc */
1+
/** @module hooks */ /** for typedoc */
22
import {TransitionStateHookFn} from "../transition/interface";
33
import {State} from "../state/stateObject";
44
import {Transition} from "../transition/transition";
55

6-
export function makeEnterExitRetainHook(hookName: string): TransitionStateHookFn {
6+
/**
7+
* A factory which creates an onEnter, onExit or onRetain transition hook function
8+
*
9+
* The returned function invokes the (for instance) state.onEnter hook when the
10+
* state is being entered.
11+
*
12+
* @hidden
13+
*/
14+
function makeEnterExitRetainHook(hookName: string): TransitionStateHookFn {
715
return (transition: Transition, state: State) =>
816
state[hookName](transition, state);
917
}
18+
19+
/**
20+
* The [[TransitionStateHookFn]] for onExit
21+
*
22+
* When the state is being exited, the state's .onExit function is invoked.
23+
*
24+
* Registered using `transitionService.onExit({ exiting: (state) => !!state.onExit }, onExitHook);`
25+
*/
26+
export const onExitHook: TransitionStateHookFn = makeEnterExitRetainHook('onExit');
27+
28+
/**
29+
* The [[TransitionStateHookFn]] for onRetain
30+
*
31+
* When the state is being exited, the state's .onRetain function is invoked.
32+
*
33+
* Registered using `transitionService.onRetain({ retained: (state) => !!state.onRetain }, onRetainHook);`
34+
*/
35+
export const onRetainHook: TransitionStateHookFn = makeEnterExitRetainHook('onRetain');
36+
37+
/**
38+
* The [[TransitionStateHookFn]] for onEnter
39+
*
40+
* When the state is being exited, the state's .onEnter function is invoked.
41+
*
42+
* Registered using `transitionService.onEnter({ entering: (state) => !!state.onEnter }, onEnterHook);`
43+
*/
44+
export const onEnterHook: TransitionStateHookFn = makeEnterExitRetainHook('onEnter');

src/hooks/redirectTo.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
/** @module hooks */ /** */
12
import {isString, isFunction} from "../common/predicates"
23
import {Transition} from "../transition/transition";
34
import {services} from "../common/coreservices";
45
import {TargetState} from "../state/targetState";
56

67
/**
7-
* A hook that redirects to a different state or params
8+
* A [[TransitionHookFn]] that redirects to a different state or params
89
*
10+
* Registered using `transitionService.onStart({ to: (state) => !!state.redirectTo }, redirectHook);`
11+
*
912
* See [[StateDeclaration.redirectTo]]
1013
*/
1114
export const redirectToHook = (trans: Transition) => {

src/hooks/resolve.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
1-
/** @module state */ /** for typedoc */
1+
/** @module hooks */ /** for typedoc */
22
import {noop} from "../common/common";
33
import {Transition} from "../transition/transition";
44
import {State} from "../state/stateObject";
55
import {ResolveContext} from "../resolve/resolveContext";
6+
import {TransitionStateHookFn, TransitionHookFn} from "../transition/interface";
67

7-
/** A function which resolves all EAGER Resolvables in the To Path */
8-
export const $eagerResolvePath = (trans: Transition) =>
8+
/**
9+
* A [[TransitionHookFn]] which resolves all EAGER Resolvables in the To Path
10+
*
11+
* Registered using `transitionService.onStart({}, eagerResolvePath);`
12+
*
13+
* When a Transition starts, this hook resolves all the EAGER Resolvables, which the transition then waits for.
14+
*
15+
* See [[StateDeclaration.resolve]]
16+
*/
17+
export const eagerResolvePath: TransitionHookFn = (trans: Transition) =>
918
new ResolveContext(trans.treeChanges().to)
1019
.resolvePath("EAGER", trans)
1120
.then(noop);
1221

13-
/** A function which resolves all LAZY Resolvables for the state (and all ancestors) in the To Path */
14-
export const $lazyResolveState = (trans: Transition, state: State) =>
22+
/**
23+
* A [[TransitionHookFn]] which resolves all LAZY Resolvables for the state (and all its ancestors) in the To Path
24+
*
25+
* Registered using `transitionService.onEnter({ entering: () => true }, lazyResolveState);`
26+
*
27+
* When a State is being entered, this hook resolves all the Resolvables for this state, which the transition then waits for.
28+
*
29+
* See [[StateDeclaration.resolve]]
30+
*/
31+
export const lazyResolveState: TransitionStateHookFn = (trans: Transition, state: State) =>
1532
new ResolveContext(trans.treeChanges().to)
1633
.subContext(state)
1734
.resolvePath("LAZY", trans)

src/hooks/transitionManager.ts

-43
This file was deleted.

src/hooks/url.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
/** @module hooks */ /** */
12
import {UrlRouter} from "../url/urlRouter";
23
import {StateService} from "../state/stateService";
34
import {Transition} from "../transition/transition";
4-
import {UiInjector} from "../common/interface";
5-
import {UiRouter} from "../router";
65

6+
/**
7+
* A [[TransitionHookFn]] which updates the URL after a successful transition
8+
*
9+
* Registered using `transitionService.onSuccess({}, updateUrl);`
10+
*/
711
export function updateUrl(transition: Transition) {
812
let options = transition.options();
913
let $state: StateService = transition.router.stateService;

src/hooks/views.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
/** @module state */ /** for typedoc */
1+
/** @module hooks */ /** for typedoc */
22
import {noop} from "../common/common";
33
import {services} from "../common/coreservices";
44
import {Transition} from "../transition/transition";
55
import {ViewService} from "../view/view";
66
import {ViewConfig} from "../view/interface";
77

88

9-
/** Allows the views to do async work [.load()] before the transition continues */
9+
/**
10+
* A [[TransitionHookFn]] which waits for the views to load
11+
*
12+
* Registered using `transitionService.onStart({}, loadEnteringViews);`
13+
*
14+
* Allows the views to do async work in [[ViewConfig.load]] before the transition continues.
15+
* In angular 1, this includes loading the templates.
16+
*/
1017
export function loadEnteringViews(transition) {
1118
let enteringViews = transition.views("entering");
1219
if (!enteringViews.length) return;
1320
return services.$q.all(enteringViews.map(view => view.load())).then(noop);
1421
}
1522

23+
/**
24+
* A [[TransitionHookFn]] which activates the new views when a transition is successful.
25+
*
26+
* Registered using `transitionService.onSuccess({}, activateViews);`
27+
*
28+
* After a transition is complete, this hook deactivates the old views from the previous state,
29+
* and activates the new views from the destination state.
30+
*
31+
* See [[ViewService]]
32+
*/
1633
export function activateViews(transition: Transition) {
1734
let enteringViews = transition.views("entering");
1835
let exitingViews = transition.views("exiting");

src/ng1/legacy/resolveService.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
1+
/** @module ng1 */ /** */
12
import {State} from "../../state/stateObject";
23
import {PathNode} from "../../path/node";
34
import {ResolveContext} from "../../resolve/resolveContext";
45
import {map} from "../../common/common";
56
import {resolvablesBuilder} from "../../state/stateBuilder";
67

7-
export const resolveFactory = () => ({
8+
/**
9+
* Implementation of the legacy `$resolve` service for angular 1.
10+
*/
11+
var $resolve = {
812
/**
13+
* Asynchronously injects a resolve block.
14+
*
915
* This emulates most of the behavior of the ui-router 0.2.x $resolve.resolve() service API.
10-
* @param invocables an object, with keys as resolve names and values as injectable functions
16+
*
17+
* Given an object `invocables`, where keys are strings and values are injectable functions,
18+
* injects each function, and waits for the resulting promise to resolve.
19+
* When all resulting promises are resolved, returns the results as an object.
20+
*
21+
* @example
22+
* ```js
23+
*
24+
* let invocables = {
25+
* foo: [ '$http', ($http) =>
26+
* $http.get('/api/foo').then(resp => resp.data) ],
27+
* bar: [ 'foo', '$http', (foo, $http) =>
28+
* $http.get('/api/bar/' + foo.barId).then(resp => resp.data) ]
29+
* }
30+
* $resolve.resolve(invocables)
31+
* .then(results => console.log(results.foo, results.bar))
32+
* // Logs foo and bar:
33+
* // { id: 123, barId: 456, fooData: 'foo data' }
34+
* // { id: 456, barData: 'bar data' }
35+
* ```
36+
*
37+
* @param invocables an object which looks like an [[StateDefinition.resolve]] object; keys are resolve names and values are injectable functions
1138
* @param locals key/value pre-resolved data (locals)
1239
* @param parent a promise for a "parent resolve"
1340
*/
@@ -32,4 +59,7 @@ export const resolveFactory = () => ({
3259

3360
return parent ? parent.then(resolveData) : resolveData({});
3461
}
35-
});
62+
};
63+
64+
/** @hidden */
65+
export const resolveFactory = () => $resolve;

src/ng1/services.ts

+18
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,26 @@ export const getLocals = (ctx: ResolveContext) => {
283283
return tuples.reduce(applyPairs, {});
284284
};
285285

286+
/** Adds the angular 1 `$injector` to the `UiInjector` interface */
286287
declare module "../common/interface" {
288+
/**
289+
* This enhances the [[common.UiInjector]] interface by adding the `$injector` service as the [[native]] injector.
290+
*/
287291
interface UiInjector {
292+
/**
293+
* The native Angular 1 `$injector` service
294+
*
295+
* When you have access to a `UiInjector`, this property will contain the native `$injector` Angular 1 service.
296+
*
297+
* @example:
298+
* ```js
299+
*
300+
* $transition.onStart({}, function(transition) {
301+
* var uiInjector = transition.injector();
302+
* var $injector = uiInjector.native;
303+
* var val = $injector.invoke(someFunction);
304+
* });
305+
*/
288306
native: IInjectorService;
289307
}
290308
}

src/ng2/location.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @module ng2 */ /** */
12
import {HashLocationStrategy, PlatformLocation, LocationStrategy} from "@angular/common";
23
import {Injectable} from "@angular/core";
34

src/state/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ export interface StateDeclaration {
479479
*/
480480
onEnter?: TransitionStateHookFn;
481481
/**
482-
* A [[TransitionStateHook]] called with the state is being retained/kept. See: [[IHookRegistry.onRetain]]
482+
* A [[TransitionStateHookFn]] called with the state is being retained/kept. See: [[IHookRegistry.onRetain]]
483483
*
484484
* @example
485485
* ```js

0 commit comments

Comments
 (0)