docs(router): improve docs for lifecycle hooks

Closes #4300
This commit is contained in:
Brian Ford 2015-09-21 16:25:37 -07:00
parent 5c0a9eff62
commit c01f327194
2 changed files with 101 additions and 38 deletions

View File

@ -8,76 +8,102 @@ var __ignore_me = global;
/** /**
* Defines route lifecycle method [onActivate], which is called by the router at the end of a * Defines route lifecycle method `onActivate`, which is called by the router at the end of a
* successful route navigation. * successful route navigation.
* *
* For a single component's navigation, only one of either [onActivate] or [onReuse] will be called, * For a single component's navigation, only one of either {@link OnActivate} or {@link OnReuse}
* depending on the result of [canReuse]. * will be called depending on the result of {@link CanReuse}.
*
* The `onActivate` hook is called with two {@link ComponentInstruction}s as parameters, the first
* representing the current route being navigated to, and the second parameter representing the
* previous route or `null`.
* *
* If `onActivate` returns a promise, the route change will wait until the promise settles to * If `onActivate` returns a promise, the route change will wait until the promise settles to
* instantiate and activate child components. * instantiate and activate child components.
* *
* ## Example * ## Example
* ``` * ```
* @Directive({ * import {Component, View} from 'angular2/angular2';
* import {OnActivate, ComponentInstruction} from 'angular2/router';
*
* @Component({
* selector: 'my-cmp' * selector: 'my-cmp'
* }) * })
* @View({
* template: '<div>hello!</div>'
* })
* class MyCmp implements OnActivate { * class MyCmp implements OnActivate {
* onActivate(next, prev) { * onActivate(next: ComponentInstruction, prev: ComponentInstruction) {
* this.log = 'Finished navigating from ' + prev.urlPath + ' to ' + next.urlPath; * this.log = 'Finished navigating from ' + prev.urlPath + ' to ' + next.urlPath;
* } * }
* } * }
* ``` * ```
*/ */
export interface OnActivate { export interface OnActivate {
onActivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any; onActivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any;
} }
/** /**
* Defines route lifecycle method [onReuse], which is called by the router at the end of a * Defines route lifecycle method `onReuse`, which is called by the router at the end of a
* successful route navigation when [canReuse] is implemented and returns or resolves to true. * successful route navigation when {@link CanReuse} is implemented and returns or resolves to true.
* *
* For a single component's navigation, only one of either [onActivate] or [onReuse] will be called, * For a single component's navigation, only one of either {@link OnActivate} or {@link OnReuse}
* depending on the result of [canReuse]. * will be called, depending on the result of {@link CanReuse}.
*
* The `onReuse` hook is called with two {@link ComponentInstruction}s as parameters, the first
* representing the current route being navigated to, and the second parameter representing the
* previous route or `null`.
* *
* ## Example * ## Example
* ``` * ```
* @Directive({ * import {Component, View} from 'angular2/angular2';
* import {CanReuse, OnReuse, ComponentInstruction} from 'angular2/router';
*
* @Component({
* selector: 'my-cmp' * selector: 'my-cmp'
* }) * })
* @View({
* template: '<div>hello!</div>'
* })
* class MyCmp implements CanReuse, OnReuse { * class MyCmp implements CanReuse, OnReuse {
* canReuse() { * canReuse(next: ComponentInstruction, prev: ComponentInstruction) {
* return true; * return true;
* } * }
* *
* onReuse(next, prev) { * onReuse(next: ComponentInstruction, prev: ComponentInstruction) {
* this.params = next.params; * this.params = next.params;
* } * }
* } * }
* ``` * ```
*/ */
export interface OnReuse { export interface OnReuse {
onReuse(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any; onReuse(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any;
} }
/** /**
* Defines route lifecycle method [onDeactivate], which is called by the router before destroying * Defines route lifecycle method `onDeactivate`, which is called by the router before destroying
* a component as part of a route change. * a component as part of a route change.
* *
* The `onDeactivate` hook is called with two {@link ComponentInstruction}s as parameters, the first
* representing the current route being navigated to, and the second parameter representing the
* previous route.
*
* If `onDeactivate` returns a promise, the route change will wait until the promise settles. * If `onDeactivate` returns a promise, the route change will wait until the promise settles.
* *
* ## Example * ## Example
* ``` * ```
* @Directive({ * import {Component, View} from 'angular2/angular2';
* import {OnDeactivate, ComponentInstruction} from 'angular2/router';
*
* @Component({
* selector: 'my-cmp' * selector: 'my-cmp'
* }) * })
* class MyCmp implements CanReuse, OnReuse { * @View({
* canReuse() { * template: '<div>hello!</div>'
* return true; * })
* } * class MyCmp implements OnDeactivate {
* * onDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
* onReuse(next, prev) { * return this.doFadeAwayAnimation();
* this.params = next.params;
* } * }
* } * }
* ``` * ```
@ -87,24 +113,37 @@ export interface OnDeactivate {
} }
/** /**
* Defines route lifecycle method [canReuse], which is called by the router to determine whether a * Defines route lifecycle method `canReuse`, which is called by the router to determine whether a
* component should be reused across routes, or whether to destroy and instantiate a new component. * component should be reused across routes, or whether to destroy and instantiate a new component.
* *
* If `canReuse` returns or resolves to `true`, the component instance will be reused. * The `canReuse` hook is called with two {@link ComponentInstruction}s as parameters, the first
* representing the current route being navigated to, and the second parameter representing the
* previous route.
*
* If `canReuse` returns or resolves to `true`, the component instance will be reused and the
* {@link OnDeactivate} hook will be run. If `canReuse` returns or resolves to `false`, a new
* component will be instantiated, and the existing component will be deactivated and removed as
* part of the navigation.
* *
* If `canReuse` throws or rejects, the navigation will be cancelled. * If `canReuse` throws or rejects, the navigation will be cancelled.
* *
* ## Example * ## Example
* ``` * ```
* @Directive({ * import {Component, View} from 'angular2/angular2';
* import {CanReuse, OnReuse, ComponentInstruction} from 'angular2/router';
*
* @Component({
* selector: 'my-cmp' * selector: 'my-cmp'
* }) * })
* @View({
* template: '<div>hello!</div>'
* })
* class MyCmp implements CanReuse, OnReuse { * class MyCmp implements CanReuse, OnReuse {
* canReuse(next, prev) { * canReuse(next: ComponentInstruction, prev: ComponentInstruction) {
* return next.params.id == prev.params.id; * return next.params.id == prev.params.id;
* } * }
* *
* onReuse(next, prev) { * onReuse(next: ComponentInstruction, prev: ComponentInstruction) {
* this.id = next.params.id; * this.id = next.params.id;
* } * }
* } * }
@ -115,20 +154,32 @@ export interface CanReuse {
} }
/** /**
* Defines route lifecycle method [canDeactivate], which is called by the router to determine * Defines route lifecycle method `canDeactivate`, which is called by the router to determine
* if a component can be removed as part of a navigation. * if a component can be removed as part of a navigation.
* *
* If `canDeactivate` returns or resolves to `false`, the navigation is cancelled. * The `canDeactivate` hook is called with two {@link ComponentInstruction}s as parameters, the
* first representing the current route being navigated to, and the second parameter
* representing the previous route.
*
* If `canDeactivate` returns or resolves to `false`, the navigation is cancelled. If it returns or
* resolves to `true`, then the navigation continues, and the component will be deactivated
* (the {@link OnDeactivate} hook will be run) and removed.
* *
* If `canDeactivate` throws or rejects, the navigation is also cancelled. * If `canDeactivate` throws or rejects, the navigation is also cancelled.
* *
* ## Example * ## Example
* ``` * ```
* @Directive({ * import {Component, View} from 'angular2/angular2';
* import {CanDeactivate, ComponentInstruction} from 'angular2/router';
*
* @Component({
* selector: 'my-cmp' * selector: 'my-cmp'
* }) * })
* @View({
* template: '<div>hello!</div>'
* })
* class MyCmp implements CanDeactivate { * class MyCmp implements CanDeactivate {
* canDeactivate(next, prev) { * canDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
* return askUserIfTheyAreSureTheyWantToQuit(); * return askUserIfTheyAreSureTheyWantToQuit();
* } * }
* } * }

View File

@ -17,21 +17,33 @@ export {
} from './lifecycle_annotations_impl'; } from './lifecycle_annotations_impl';
/** /**
* Defines route lifecycle method [canActivate], which is called by the router to determine * Defines route lifecycle hook `CanActivate`, which is called by the router to determine
* if a component can be instantiated as part of a navigation. * if a component can be instantiated as part of a navigation.
* *
* The `CanActivate` hook is called with two {@link ComponentInstruction}s as parameters, the first
* representing
* the current route being navigated to, and the second parameter representing the previous route or
* `null`.
*
* Note that unlike other lifecycle hooks, this one uses an annotation rather than an interface. * Note that unlike other lifecycle hooks, this one uses an annotation rather than an interface.
* This is because [canActivate] is called before the component is instantiated. * This is because the `CanActivate` function is called before the component is instantiated.
* *
* If `canActivate` returns or resolves to `false`, the navigation is cancelled. * If `CanActivate` returns or resolves to `false`, the navigation is cancelled.
* * If `CanActivate` throws or rejects, the navigation is also cancelled.
* If `canActivate` throws or rejects, the navigation is also cancelled. * If `CanActivate` returns or resolves to `true`, navigation continues, the component is
* instantiated, and the {@link OnActivate} hook of that component is called if implemented.
* *
* ## Example * ## Example
* ``` * ```
* @Directive({ * import {Component} from 'angular2/angular2';
* import {CanActivate} from 'angular2/router';
*
* @Component({
* selector: 'control-panel-cmp' * selector: 'control-panel-cmp'
* }) * })
* @View({
* template: '<div>Control Panel: ...</div>'
* })
* @CanActivate(() => checkIfUserIsLoggedIn()) * @CanActivate(() => checkIfUserIsLoggedIn())
* class ControlPanelCmp { * class ControlPanelCmp {
* // ... * // ...