refactor(LifecycleEvent): change from onInit to Lifecycle.onInit

BREAKING CHANGE

Closes #2928
This commit is contained in:
Misko Hevery
2015-07-04 15:04:50 +02:00
committed by Tobias Bosch
parent e1e7912ab2
commit b73ba68215
31 changed files with 245 additions and 250 deletions

View File

@ -6,10 +6,5 @@
export {
Component as ComponentAnnotation,
Directive as DirectiveAnnotation,
LifecycleEvent,
onDestroy,
onChange,
onCheck,
onInit,
onAllChangesDone
LifecycleEvent
} from '../annotations_impl/annotations';

View File

@ -933,130 +933,132 @@ export class Component extends Directive {
* - `onCheck`,
* - `onAllChangesDone`
*/
@CONST()
export class LifecycleEvent {
constructor(public name: string) {}
export enum LifecycleEvent {
/**
* Notify a directive whenever a {@link View} that contains it is destroyed.
*
* ## Example
*
* ```
* @Directive({
* ...,
* lifecycle: [LifecycleEvent.onDestroy]
* })
* class ClassSet {
* onDestroy() {
* // invoked to notify directive of the containing view destruction.
* }
* }
* ```
* @exportedAs angular2/annotations
*/
onDestroy,
/**
* Notify a directive when any of its bindings have changed.
*
* This method 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:
*
* ```
* @Directive({
* selector: '[class-set]',
* properties: [
* 'propA',
* 'propB'
* ],
* lifecycle: [LifecycleEvent.onChange]
* })
* class ClassSet {
* propA;
* propB;
* onChange(changes:{[idx: string, PropertyUpdate]}) {
* // This will get called after any of the properties have been updated.
* if (changes['propA']) {
* // if propA was updated
* }
* if (changes['propA']) {
* // if propB was updated
* }
* }
* }
* ```
* @exportedAs angular2/annotations
*/
onChange,
/**
* Notify a directive when it has been checked.
*
* This method 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 every time even when none of the directive's bindings has changed.
*
* ## Example:
*
* ```
* @Directive({
* selector: '[class-set]',
* lifecycle: [LifecycleEvent.onCheck]
* })
* class ClassSet {
* onCheck() {
* }
* }
* ```
* @exportedAs angular2/annotations
*/
onCheck,
/**
* Notify a directive when it has been checked the first itme.
*
* This method 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 once.
*
* ## Example:
*
* ```
* @Directive({
* selector: '[class-set]',
* lifecycle: [LifecycleEvent.onInit]
* })
* class ClassSet {
* onInit() {
* }
* }
* ```
* @exportedAs angular2/annotations
*/
onInit,
/**
* Notify a directive when the bindings of all its children have been checked (whether they have
* changed or not).
*
* ## Example:
*
* ```
* @Directive({
* selector: '[class-set]',
* lifecycle: [LifecycleEvent.onAllChangesDone]
* })
* class ClassSet {
*
* onAllChangesDone() {
* }
*
* }
* ```
* @exportedAs angular2/annotations
*/
onAllChangesDone
}
/**
* Notify a directive whenever a {@link View} that contains it is destroyed.
*
* ## Example
*
* ```
* @Directive({
* ...,
* lifecycle: [onDestroy]
* })
* class ClassSet {
* onDestroy() {
* // invoked to notify directive of the containing view destruction.
* }
* }
* ```
*/
export const onDestroy: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onDestroy"));
/**
* Notify a directive when any of its bindings have changed.
*
* This method 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:
*
* ```
* @Directive({
* selector: '[class-set]',
* properties: [
* 'propA',
* 'propB'
* ],
* lifecycle: [onChange]
* })
* class ClassSet {
* propA;
* propB;
* onChange(changes:{[idx: string, PropertyUpdate]}) {
* // This will get called after any of the properties have been updated.
* if (changes['propA']) {
* // if propA was updated
* }
* if (changes['propA']) {
* // if propB was updated
* }
* }
* }
* ```
*/
export const onChange: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onChange"));
/**
* Notify a directive when it has been checked.
*
* This method 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 every time even when none of the directive's bindings has changed.
*
* ## Example:
*
* ```
* @Directive({
* selector: '[class-set]',
* lifecycle: [onCheck]
* })
* class ClassSet {
* onCheck() {
* }
* }
* ```
*/
export const onCheck: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onCheck"));
/**
* Notify a directive when it has been checked the first itme.
*
* This method 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 once.
*
* ## Example:
*
* ```
* @Directive({
* selector: '[class-set]',
* lifecycle: [onInit]
* })
* class ClassSet {
* onInit() {
* }
* }
* ```
*/
export const onInit: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onInit"));
/**
* Notify a directive when the bindings of all its children have been checked (whether they have
* changed or not).
*
* ## Example:
*
* ```
* @Directive({
* selector: '[class-set]',
* lifecycle: [onAllChangesDone]
* })
* class ClassSet {
*
* onAllChangesDone() {
* }
*
* }
* ```
*/
export const onAllChangesDone: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onAllChangesDone"));

View File

@ -11,15 +11,15 @@ bool hasLifecycleHook(LifecycleEvent e, type, Directive annotation) {
final List interfaces = reflector.interfaces(type);
var interface;
if (e == onChange) {
if (e == LifecycleEvent.onChange) {
interface = OnChange;
} else if (e == onDestroy) {
} else if (e == LifecycleEvent.onDestroy) {
interface = OnDestroy;
} else if (e == onAllChangesDone) {
} else if (e == LifecycleEvent.onAllChangesDone) {
interface = OnAllChangesDone;
} else if (e == onCheck) {
} else if (e == LifecycleEvent.onCheck) {
interface = OnCheck;
} else if (e == onInit) {
} else if (e == LifecycleEvent.onInit) {
interface = OnInit;
}

View File

@ -6,6 +6,20 @@ export function hasLifecycleHook(e: LifecycleEvent, type, annotation: Directive)
return annotation.lifecycle.indexOf(e) !== -1;
} else {
if (!(type instanceof Type)) return false;
return e.name in(<any>type).prototype;
var proto = (<any>type).prototype;
switch (e) {
case LifecycleEvent.onAllChangesDone:
return !!proto.onAllChangesDone;
case LifecycleEvent.onChange:
return !!proto.onChange;
case LifecycleEvent.onCheck:
return !!proto.onCheck;
case LifecycleEvent.onDestroy:
return !!proto.onDestroy;
case LifecycleEvent.onInit:
return !!proto.onInit;
default:
return false;
}
}
}
}

View File

@ -41,15 +41,7 @@ import * as avmModule from './view_manager';
import {ViewContainerRef} from './view_container_ref';
import {ElementRef} from './element_ref';
import {ProtoViewRef, ViewRef} from './view_ref';
import {
Directive,
Component,
onChange,
onDestroy,
onCheck,
onInit,
onAllChangesDone
} from 'angular2/src/core/annotations_impl/annotations';
import {Directive, Component, LifecycleEvent} from 'angular2/src/core/annotations_impl/annotations';
import {hasLifecycleHook} from './directive_lifecycle_reflector';
import {ChangeDetector, ChangeDetectorRef, Pipes} from 'angular2/change_detection';
import {QueryList} from './query_list';
@ -253,11 +245,11 @@ export class DirectiveBinding extends ResolvedBinding {
properties: ann.properties,
readAttributes: DirectiveBinding._readAttributes(deps),
callOnDestroy: hasLifecycleHook(onDestroy, rb.key.token, ann),
callOnChange: hasLifecycleHook(onChange, rb.key.token, ann),
callOnCheck: hasLifecycleHook(onCheck, rb.key.token, ann),
callOnInit: hasLifecycleHook(onInit, rb.key.token, ann),
callOnAllChangesDone: hasLifecycleHook(onAllChangesDone, rb.key.token, ann),
callOnDestroy: hasLifecycleHook(LifecycleEvent.onDestroy, rb.key.token, ann),
callOnChange: hasLifecycleHook(LifecycleEvent.onChange, rb.key.token, ann),
callOnCheck: hasLifecycleHook(LifecycleEvent.onCheck, rb.key.token, ann),
callOnInit: hasLifecycleHook(LifecycleEvent.onInit, rb.key.token, ann),
callOnAllChangesDone: hasLifecycleHook(LifecycleEvent.onAllChangesDone, rb.key.token, ann),
changeDetection: ann instanceof Component ? ann.changeDetection : null,