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
@@ -6,10 +6,5 @@
export {
Component as ComponentAnnotation,
Directive as DirectiveAnnotation,
LifecycleEvent,
onDestroy,
onChange,
onCheck,
onInit,
onAllChangesDone
LifecycleEvent
} from '../annotations_impl/annotations';
@@ -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"));
@@ -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;
}
@@ -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;
}
}
}
}
@@ -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,
+3 -2
View File
@@ -1,4 +1,4 @@
import {Directive, onCheck} from 'angular2/annotations';
import {Directive, LifecycleEvent} from 'angular2/annotations';
import {ElementRef} from 'angular2/core';
import {Pipes} from 'angular2/src/change_detection/pipes/pipes';
import {Pipe} from 'angular2/src/change_detection/pipes/pipe';
@@ -28,7 +28,8 @@ import {ListWrapper, StringMapWrapper, isListLikeIterable} from 'angular2/src/fa
* </div>
* ```
*/
@Directive({selector: '[class]', lifecycle: [onCheck], properties: ['rawClass: class']})
@Directive(
{selector: '[class]', lifecycle: [LifecycleEvent.onCheck], properties: ['rawClass: class']})
export class CSSClass {
_pipe: Pipe;
_rawClass;
+10 -2
View File
@@ -1,5 +1,12 @@
import {Directive} from 'angular2/annotations';
import {ViewContainerRef, ViewRef, ProtoViewRef, Pipes, onCheck, Pipe} from 'angular2/angular2';
import {
ViewContainerRef,
ViewRef,
ProtoViewRef,
Pipes,
LifecycleEvent,
Pipe
} from 'angular2/angular2';
import {isPresent, isBlank} from 'angular2/src/facade/lang';
/**
@@ -32,7 +39,8 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
* - `<li template="ng-for #item of items; #i = index">...</li>`
* - `<template ng-for #item [ng-for-of]="items" #i="index"><li>...</li></template>`
*/
@Directive({selector: '[ng-for][ng-for-of]', properties: ['ngForOf'], lifecycle: [onCheck]})
@Directive(
{selector: '[ng-for][ng-for-of]', properties: ['ngForOf'], lifecycle: [LifecycleEvent.onCheck]})
export class NgFor {
_ngForOf: any;
_pipe: Pipe;
+6 -2
View File
@@ -1,4 +1,4 @@
import {Directive, onCheck} from 'angular2/annotations';
import {Directive, LifecycleEvent} from 'angular2/annotations';
import {ElementRef} from 'angular2/core';
import {Pipe} from 'angular2/src/change_detection/pipes/pipe';
import {Pipes} from 'angular2/src/change_detection/pipes/pipes';
@@ -27,7 +27,11 @@ import {Renderer} from 'angular2/src/render/api';
* - `<div ng-style="{'text-align': alignEpr}"></div>`
* - `<div ng-style="styleExp"></div>`
*/
@Directive({selector: '[ng-style]', lifecycle: [onCheck], properties: ['rawStyle: ng-style']})
@Directive({
selector: '[ng-style]',
lifecycle: [LifecycleEvent.onCheck],
properties: ['rawStyle: ng-style']
})
export class NgStyle {
_pipe: Pipe;
_rawStyle;
@@ -1,4 +1,4 @@
import {Directive, onDestroy, onInit} from 'angular2/annotations';
import {Directive, LifecycleEvent} from 'angular2/angular2';
import {Inject, Ancestor, forwardRef, Binding} from 'angular2/di';
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {CONST_EXPR} from 'angular2/src/facade/lang';
@@ -52,7 +52,7 @@ const controlGroupBinding =
selector: '[ng-control-group]',
hostInjector: [controlGroupBinding],
properties: ['name: ng-control-group'],
lifecycle: [onInit, onDestroy],
lifecycle: [LifecycleEvent.onInit, LifecycleEvent.onDestroy],
exportAs: 'form'
})
export class NgControlGroup extends ControlContainer {
@@ -71,4 +71,4 @@ export class NgControlGroup extends ControlContainer {
get path(): List<string> { return controlPath(this.name, this._parent); }
get formDirective(): Form { return this._parent.formDirective; }
}
}
@@ -2,8 +2,7 @@ import {CONST_EXPR} from 'angular2/src/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {List, StringMapWrapper, StringMap} from 'angular2/src/facade/collection';
import {Directive, Query, onDestroy, onChange} from 'angular2/annotations';
import {QueryList} from 'angular2/core';
import {Directive, LifecycleEvent, Query, QueryList} from 'angular2/angular2';
import {forwardRef, Ancestor, Binding, Inject} from 'angular2/di';
import {ControlContainer} from './control_container';
@@ -76,7 +75,7 @@ const controlNameBinding =
hostInjector: [controlNameBinding],
properties: ['name: ngControl', 'model: ngModel'],
events: ['update: ngModel'],
lifecycle: [onDestroy, onChange],
lifecycle: [LifecycleEvent.onDestroy, LifecycleEvent.onChange],
exportAs: 'form'
})
export class NgControlName extends NgControl {
@@ -115,4 +114,4 @@ export class NgControlName extends NgControl {
get control(): Control { return this.formDirective.getControl(this); }
get validator(): Function { return composeNgValidator(this.ngValidators); }
}
}
@@ -2,8 +2,7 @@ import {CONST_EXPR} from 'angular2/src/facade/lang';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {Directive, Query, onChange} from 'angular2/annotations';
import {QueryList} from 'angular2/core';
import {Directive, LifecycleEvent, Query, QueryList} from 'angular2/angular2';
import {forwardRef, Ancestor, Binding} from 'angular2/di';
import {NgControl} from './ng_control';
@@ -64,7 +63,7 @@ const formControlBinding =
hostInjector: [formControlBinding],
properties: ['form: ngFormControl', 'model: ngModel'],
events: ['update: ngModel'],
lifecycle: [onChange],
lifecycle: [LifecycleEvent.onChange],
exportAs: 'form'
})
export class NgFormControl extends NgControl {
@@ -2,7 +2,7 @@ import {CONST_EXPR} from 'angular2/src/facade/lang';
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {ObservableWrapper, EventEmitter} from 'angular2/src/facade/async';
import {Directive, onChange} from 'angular2/annotations';
import {Directive, LifecycleEvent} from 'angular2/angular2';
import {forwardRef, Binding} from 'angular2/di';
import {NgControl} from './ng_control';
import {NgControlGroup} from './ng_control_group';
@@ -84,7 +84,7 @@ const formDirectiveBinding =
selector: '[ng-form-model]',
hostInjector: [formDirectiveBinding],
properties: ['form: ng-form-model'],
lifecycle: [onChange],
lifecycle: [LifecycleEvent.onChange],
host: {
'(submit)': 'onSubmit()',
},
@@ -2,8 +2,7 @@ import {CONST_EXPR} from 'angular2/src/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import {Directive, Query, onChange} from 'angular2/annotations';
import {QueryList} from 'angular2/core';
import {Directive, LifecycleEvent, QueryList, Query} from 'angular2/angular2';
import {forwardRef, Ancestor, Binding} from 'angular2/di';
import {NgControl} from './ng_control';
@@ -34,7 +33,7 @@ const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRe
hostInjector: [formControlBinding],
properties: ['model: ngModel'],
events: ['update: ngModel'],
lifecycle: [onChange],
lifecycle: [LifecycleEvent.onChange],
exportAs: 'form'
})
export class NgModel extends NgControl {
@@ -1,6 +1,5 @@
import {Renderer} from 'angular2/render';
import {ElementRef, QueryList} from 'angular2/core';
import {Directive, Query, onDestroy, onChange} from 'angular2/annotations';
import {ElementRef, QueryList, Directive, Query} from 'angular2/angular2';
import {NgControl} from './ng_control';
import {ControlValueAccessor} from './control_value_accessor';
@@ -261,7 +261,7 @@ class _DirectiveMetadataVisitor extends Object
'$lifecycleValue');
}
ListLiteral l = lifecycleValue;
var lifecycleEvents = l.elements.map((s) => s.toSource());
var lifecycleEvents = l.elements.map((s) => s.toSource().split('.').last);
_callOnDestroy = lifecycleEvents.contains("onDestroy");
_callOnChange = lifecycleEvents.contains("onChange");
_callOnCheck = lifecycleEvents.contains("onCheck");