2015-09-18 13:33:23 -04:00
|
|
|
import {StringMap, MapWrapper} from 'angular2/src/core/facade/collection';
|
2015-09-23 14:43:31 -04:00
|
|
|
import {SimpleChange} from 'angular2/src/core/change_detection/change_detection_util';
|
2015-05-22 16:14:59 -04:00
|
|
|
|
2015-09-11 17:23:24 -04:00
|
|
|
export enum LifecycleHooks {
|
|
|
|
OnInit,
|
|
|
|
OnDestroy,
|
|
|
|
DoCheck,
|
|
|
|
OnChanges,
|
|
|
|
AfterContentInit,
|
|
|
|
AfterContentChecked,
|
|
|
|
AfterViewInit,
|
|
|
|
AfterViewChecked
|
|
|
|
}
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
export var LIFECYCLE_HOOKS_VALUES = [
|
|
|
|
LifecycleHooks.OnInit,
|
|
|
|
LifecycleHooks.OnDestroy,
|
|
|
|
LifecycleHooks.DoCheck,
|
|
|
|
LifecycleHooks.OnChanges,
|
|
|
|
LifecycleHooks.AfterContentInit,
|
|
|
|
LifecycleHooks.AfterContentChecked,
|
|
|
|
LifecycleHooks.AfterViewInit,
|
|
|
|
LifecycleHooks.AfterViewChecked
|
|
|
|
];
|
|
|
|
|
2015-05-22 16:14:59 -04:00
|
|
|
/**
|
2015-08-31 21:32:32 -04:00
|
|
|
* Lifecycle hooks are guaranteed to be called in the following order:
|
|
|
|
* - `OnChanges` (if any bindings have changed),
|
|
|
|
* - `OnInit` (after the first check only),
|
|
|
|
* - `DoCheck`,
|
|
|
|
* - `AfterContentInit`,
|
|
|
|
* - `AfterContentChecked`,
|
|
|
|
* - `OnDestroy` (at the very end before destruction)
|
2015-05-22 16:14:59 -04:00
|
|
|
*/
|
2015-05-27 11:07:37 -04:00
|
|
|
|
|
|
|
/**
|
2015-08-31 21:32:32 -04:00
|
|
|
* Notify a directive when any of its bindings have changed.
|
|
|
|
*
|
|
|
|
* `onChanges` is called right after the directive's bindings have been checked,
|
|
|
|
* and before any of its children's bindings have been checked.
|
|
|
|
*
|
|
|
|
* It is invoked only if at least one of the directive's bindings has changed.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component(...)
|
|
|
|
* class MyComponent implements OnChanges {
|
|
|
|
* propA;
|
|
|
|
* propB;
|
|
|
|
*
|
|
|
|
* onChanges(changes: {[idx: string, PropertyUpdate]}): void {
|
2015-09-30 23:59:23 -04:00
|
|
|
* // This will get called after any of the inputs have been updated.
|
2015-08-31 21:32:32 -04:00
|
|
|
* if (changes['propA']) {
|
|
|
|
* // if propA was updated
|
|
|
|
* }
|
|
|
|
* if (changes['propA']) {
|
|
|
|
* // if propB was updated
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-05-27 11:07:37 -04:00
|
|
|
*/
|
2015-09-23 14:43:31 -04:00
|
|
|
export interface OnChanges { onChanges(changes: StringMap<string, SimpleChange>); }
|
2015-05-27 11:07:37 -04:00
|
|
|
|
2015-05-27 13:14:37 -04:00
|
|
|
/**
|
2015-08-31 21:32:32 -04:00
|
|
|
* Notify a directive when it has been checked the first time.
|
|
|
|
*
|
|
|
|
* `onInit` is called right after the directive's bindings have been checked for the first time,
|
|
|
|
* and before any of its children's bindings have been checked.
|
|
|
|
*
|
|
|
|
* It is invoked only once.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component(...)
|
2015-09-11 17:23:24 -04:00
|
|
|
* class MyComponent implements OnInit {
|
2015-08-31 21:32:32 -04:00
|
|
|
* onInit(): void {
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-05-27 13:14:37 -04:00
|
|
|
*/
|
2015-09-11 17:23:24 -04:00
|
|
|
export interface OnInit { onInit(); }
|
2015-05-27 13:14:37 -04:00
|
|
|
|
|
|
|
/**
|
2015-08-31 21:32:32 -04:00
|
|
|
* Overrides the default change detection.
|
|
|
|
*
|
|
|
|
* `doCheck()` gets called to check the changes in the directives instead of the default
|
|
|
|
* change detection mechanism.
|
|
|
|
*
|
|
|
|
* It is invoked every time the change detection is triggered.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component(...)
|
|
|
|
* class MyComponent implements DoCheck {
|
|
|
|
* doCheck(): void {
|
|
|
|
* // Custom logic to detect changes
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-05-27 13:14:37 -04:00
|
|
|
*/
|
2015-09-11 17:23:24 -04:00
|
|
|
export interface DoCheck { doCheck(); }
|
2015-05-27 13:14:37 -04:00
|
|
|
|
2015-08-28 21:11:04 -04:00
|
|
|
/**
|
2015-08-31 21:32:32 -04:00
|
|
|
* Notify a directive whenever a {@link ViewMetadata} that contains it is destroyed.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component(...)
|
|
|
|
* class MyComponent implements OnDestroy {
|
|
|
|
* onDestroy(): void {
|
|
|
|
* // invoked to notify directive of the containing view destruction.
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-08-28 21:11:04 -04:00
|
|
|
*/
|
2015-09-11 17:23:24 -04:00
|
|
|
export interface OnDestroy { onDestroy(); }
|
2015-08-28 21:11:04 -04:00
|
|
|
|
2015-05-27 11:07:37 -04:00
|
|
|
/**
|
2015-08-31 21:32:32 -04:00
|
|
|
* Notify a directive when the bindings of all its content children have been checked the first
|
|
|
|
* time (whether they have changed or not).
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component(...)
|
|
|
|
* class MyComponent implements AfterContentInit {
|
|
|
|
* afterContentInit(): void {
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-05-27 11:07:37 -04:00
|
|
|
*/
|
2015-09-11 17:23:24 -04:00
|
|
|
export interface AfterContentInit { afterContentInit(); }
|
2015-08-28 21:11:04 -04:00
|
|
|
|
|
|
|
/**
|
2015-08-31 21:32:32 -04:00
|
|
|
* Notify a directive when the bindings of all its content children have been checked (whether
|
|
|
|
* they have changed or not).
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component(...)
|
|
|
|
* class MyComponent implements AfterContentChecked {
|
|
|
|
* afterContentChecked(): void {
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-08-28 21:11:04 -04:00
|
|
|
*/
|
2015-09-11 17:23:24 -04:00
|
|
|
export interface AfterContentChecked { afterContentChecked(); }
|
2015-08-28 21:11:04 -04:00
|
|
|
|
|
|
|
/**
|
2015-08-31 21:32:32 -04:00
|
|
|
* Notify a directive when the bindings of all its view children have been checked the first time
|
|
|
|
* (whether they have changed or not).
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component(...)
|
|
|
|
* class MyComponent implements AfterViewInit {
|
|
|
|
* afterViewInit(): void {
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-08-28 21:11:04 -04:00
|
|
|
*/
|
2015-09-11 17:23:24 -04:00
|
|
|
export interface AfterViewInit { afterViewInit(); }
|
2015-08-31 21:32:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify a directive when the bindings of all its view children have been checked (whether they
|
|
|
|
* have changed or not).
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component(...)
|
|
|
|
* class MyComponent implements AfterViewChecked {
|
|
|
|
* afterViewChecked(): void {
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
2015-09-11 17:23:24 -04:00
|
|
|
export interface AfterViewChecked { afterViewChecked(); }
|