Skip to content

Commit 5304f5e

Browse files
docs(hooks): cleaned up some hook docs
1 parent 0770fd8 commit 5304f5e

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

src/hooks/onEnterExitRetain.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,29 @@ function makeEnterExitRetainHook(hookName: string): TransitionStateHookFn {
2222
* When the state is being exited, the state's .onExit function is invoked.
2323
*
2424
* Registered using `transitionService.onExit({ exiting: (state) => !!state.onExit }, onExitHook);`
25+
*
26+
* See: [[IHookRegistry.onExit]]
2527
*/
2628
export const onExitHook: TransitionStateHookFn = makeEnterExitRetainHook('onExit');
2729

2830
/**
2931
* The [[TransitionStateHookFn]] for onRetain
3032
*
31-
* When the state is being exited, the state's .onRetain function is invoked.
33+
* When the state was already entered, and is not being exited or re-entered, the state's .onRetain function is invoked.
3234
*
3335
* Registered using `transitionService.onRetain({ retained: (state) => !!state.onRetain }, onRetainHook);`
36+
*
37+
* See: [[IHookRegistry.onRetain]]
3438
*/
3539
export const onRetainHook: TransitionStateHookFn = makeEnterExitRetainHook('onRetain');
3640

3741
/**
3842
* The [[TransitionStateHookFn]] for onEnter
3943
*
40-
* When the state is being exited, the state's .onEnter function is invoked.
44+
* When the state is being entered, the state's .onEnter function is invoked.
4145
*
4246
* Registered using `transitionService.onEnter({ entering: (state) => !!state.onEnter }, onEnterHook);`
47+
*
48+
* See: [[IHookRegistry.onEnter]]
4349
*/
4450
export const onEnterHook: TransitionStateHookFn = makeEnterExitRetainHook('onEnter');

src/transition/interface.ts

+38-34
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export interface TransitionHookFn {
205205
*
206206
* - [[IHookRegistry.onExit]]
207207
* - [[IHookRegistry.onRetain]]
208-
* - [[IHookRegistry.onExit]]
208+
* - [[IHookRegistry.onEnter]]
209209
*/
210210
export interface TransitionStateHookFn {
211211
(transition: Transition, state: State) : HookResult
@@ -291,12 +291,12 @@ export interface IHookRegistry {
291291
*
292292
* This example redirects any transition from 'home' to 'home.dashboard'. This is commonly referred to as a
293293
* "default substate".
294-
* @example (ng2)
295-
* ```
296294
*
297-
* transitionService.onBefore({ to: 'home' }, function(trans: Transition, injector: Injector) {
298-
* return injector.get('$state').target("home.dashboard");
299-
* });
295+
* @example
296+
* ```js
297+
* // ng2
298+
* transitionService.onBefore({ to: 'home' }, (trans: Transition) =>
299+
* trans.router.stateService.target("home.dashboard"));
300300
* ```
301301
*
302302
* #### Data Driven Default Substate
@@ -305,9 +305,9 @@ export interface IHookRegistry {
305305
* which has `defaultSubstate: "some.sub.state"` defined. See: [[Transition.to]] which returns the "to state"
306306
* definition.
307307
*
308-
* @example (ng1)
309-
* ```
310-
*
308+
* @example
309+
* ```js
310+
* // ng1
311311
* // state declaration
312312
* {
313313
* name: 'home',
@@ -320,25 +320,26 @@ export interface IHookRegistry {
320320
* return state.defaultSubstate != null;
321321
* }
322322
* }
323-
* $transitions.onBefore(criteria, function(trans: Transition, $injector) {
324-
* return $injector.get("$state").target(trans.to().defaultSubstate);
323+
*
324+
* $transitions.onBefore(criteria, function(trans: Transition) {
325+
* var substate = trans.to().defaultSubstate;
326+
* return trans.router.stateService.target(substate);
325327
* });
326328
* ```
327329
*
328330
*
329331
* #### Require authentication
330332
*
331-
* This example cancels a transition to a state which requires authentication, if the user is
332-
* not currently authenticated.
333+
* This example cancels a transition to a state which requires authentication, if the user is not currently authenticated.
333334
*
334-
* This example assumes a state tree where all states which require authentication are children of
335-
* a parent `'requireauth'` state. This example assumes `MyAuthService` synchronously returns a boolean from
336-
* `isAuthenticated()`.
337-
* @example (ng1)
338-
* ```
335+
* This example assumes a state tree where all states which require authentication are children of a parent `'requireauth'` state.
336+
* This example assumes `MyAuthService` synchronously returns a boolean from `isAuthenticated()`.
339337
*
340-
* $transitions.onBefore( { to: 'requireauth.*', from: '*' }, function(trans, $inj) {
341-
* var myAuthService = $inj.get('MyAuthService');
338+
* @example
339+
* ```js
340+
* // ng1
341+
* $transitions.onBefore( { to: 'requireauth.**' }, function(trans) {
342+
* var myAuthService = trans.injector().get('MyAuthService');
342343
* // If isAuthenticated returns false, the transition is cancelled.
343344
* return myAuthService.isAuthenticated();
344345
* });
@@ -389,12 +390,12 @@ export interface IHookRegistry {
389390
* - `MyAuthService.authenticate()` presents a login dialog, and returns a promise which is resolved
390391
* or rejected, whether or not the login attempt was successful.
391392
*
392-
* @example (ng1)
393-
* ```
394-
*
395-
* $transitions.onStart( { to: 'auth.*' }, function(trans, $injector) {
396-
* var $state = $injector.get('$state');
397-
* var MyAuthService = $injector.get('MyAuthService');
393+
* @example
394+
* ```js
395+
* // ng1
396+
* $transitions.onStart( { to: 'auth.*' }, function(trans) {
397+
* var $state = trans.router.stateService;
398+
* var MyAuthService = trans.injector().get('MyAuthService');
398399
*
399400
* // If the user is not authenticated
400401
* if (!MyAuthService.isAuthenticated()) {
@@ -464,8 +465,8 @@ export interface IHookRegistry {
464465
* @example
465466
* ```
466467
*
467-
* $transitions.onEnter({ entering: 'admin' }, function(transition, injector, state) {
468-
* var AuditService = injector.get('AuditService');
468+
* $transitions.onEnter({ entering: 'admin' }, function(transition, state) {
469+
* var AuditService = trans.injector().get('AuditService');
469470
* AuditService.log("Entered " + state.name + " module while transitioning to " + transition.to().name);
470471
* }
471472
* ```
@@ -476,14 +477,16 @@ export interface IHookRegistry {
476477
* ```
477478
* {
478479
* name: 'admin',
479-
* template: '<div ui-view/>',
480-
* onEnter: function(transition, injector, state) {
481-
* var AuditService = injector.get('AuditService');
480+
* component: 'admin',
481+
* onEnter: function($transition$, $state$) {
482+
* var AuditService = $transition$.injector().get('AuditService');
482483
* AuditService.log("Entered " + state.name + " module while transitioning to " + transition.to().name);
483484
* }
484485
* }
485486
* ```
486487
*
488+
* Note: A state declaration's `onEnter` function is injected for Angular 1 only.
489+
*
487490
* @param matchCriteria defines which Transitions the Hook should be invoked for.
488491
* @param callback the hook function which will be injected and invoked.
489492
* @returns a function which deregisters the hook.
@@ -520,6 +523,7 @@ export interface IHookRegistry {
520523
* Instead of registering `onRetain` hooks using the [[TransitionService]], you may define an `onRetain` hook
521524
* directly on a state declaration (see: [[StateDeclaration.onRetain]]).
522525
*
526+
* Note: A state declaration's `onRetain` function is injected for Angular 1 only.
523527
*
524528
* @param matchCriteria defines which Transitions the Hook should be invoked for.
525529
* @param callback the hook function which will be injected and invoked.
@@ -558,6 +562,8 @@ export interface IHookRegistry {
558562
* Instead of registering `onExit` hooks using the [[TransitionService]], you may define an `onExit` hook
559563
* directly on a state declaration (see: [[StateDeclaration.onExit]]).
560564
*
565+
* Note: A state declaration's `onExit` function is injected for Angular 1 only.
566+
*
561567
* @param matchCriteria defines which Transitions the Hook should be invoked for.
562568
* @param callback the hook function which will be injected and invoked.
563569
* @returns a function which deregisters the hook.
@@ -576,11 +582,9 @@ export interface IHookRegistry {
576582
*
577583
* ### Lifecycle
578584
*
579-
* `onFinish` hooks are invoked asynchronously, in priority order, just before the Transition completes, after
580-
* all states are entered and exited.
581585
* `onFinish` hooks are invoked after the `onEnter` phase is complete.
582586
* These hooks are invoked just before the transition is "committed".
583-
* The registered `onFinish` hooks are invoked in priority order.
587+
* Each hook is invoked in priority order.
584588
*
585589
* ### Return value
586590
*

0 commit comments

Comments
 (0)