Skip to content

Commit b73ba68

Browse files
mheverytbosch
authored andcommitted
refactor(LifecycleEvent): change from onInit to Lifecycle.onInit
BREAKING CHANGE Closes angular#2928
1 parent e1e7912 commit b73ba68

31 files changed

+241
-246
lines changed

modules/angular2/annotations.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
export {
1414
ComponentAnnotation,
1515
DirectiveAnnotation,
16-
LifecycleEvent,
17-
onDestroy,
18-
onChange,
19-
onCheck,
20-
onInit,
21-
onAllChangesDone
16+
LifecycleEvent
2217
} from './src/core/annotations/annotations';
2318

2419
export {ViewAnnotation} from 'angular2/src/core/annotations/view';

modules/angular2/src/core/annotations/annotations.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,5 @@
66
export {
77
Component as ComponentAnnotation,
88
Directive as DirectiveAnnotation,
9-
LifecycleEvent,
10-
onDestroy,
11-
onChange,
12-
onCheck,
13-
onInit,
14-
onAllChangesDone
9+
LifecycleEvent
1510
} from '../annotations_impl/annotations';

modules/angular2/src/core/annotations_impl/annotations.ts

Lines changed: 124 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -933,130 +933,132 @@ export class Component extends Directive {
933933
* - `onCheck`,
934934
* - `onAllChangesDone`
935935
*/
936-
@CONST()
937-
export class LifecycleEvent {
938-
constructor(public name: string) {}
939-
}
940-
941-
/**
942-
* Notify a directive whenever a {@link View} that contains it is destroyed.
943-
*
944-
* ## Example
945-
*
946-
* ```
947-
* @Directive({
948-
* ...,
949-
* lifecycle: [onDestroy]
950-
* })
951-
* class ClassSet {
952-
* onDestroy() {
953-
* // invoked to notify directive of the containing view destruction.
954-
* }
955-
* }
956-
* ```
957-
*/
958-
export const onDestroy: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onDestroy"));
936+
export enum LifecycleEvent {
937+
/**
938+
* Notify a directive whenever a {@link View} that contains it is destroyed.
939+
*
940+
* ## Example
941+
*
942+
* ```
943+
* @Directive({
944+
* ...,
945+
* lifecycle: [LifecycleEvent.onDestroy]
946+
* })
947+
* class ClassSet {
948+
* onDestroy() {
949+
* // invoked to notify directive of the containing view destruction.
950+
* }
951+
* }
952+
* ```
953+
* @exportedAs angular2/annotations
954+
*/
955+
onDestroy,
959956

960957

961-
/**
962-
* Notify a directive when any of its bindings have changed.
963-
*
964-
* This method is called right after the directive's bindings have been checked,
965-
* and before any of its children's bindings have been checked.
966-
*
967-
* It is invoked only if at least one of the directive's bindings has changed.
968-
*
969-
* ## Example:
970-
*
971-
* ```
972-
* @Directive({
973-
* selector: '[class-set]',
974-
* properties: [
975-
* 'propA',
976-
* 'propB'
977-
* ],
978-
* lifecycle: [onChange]
979-
* })
980-
* class ClassSet {
981-
* propA;
982-
* propB;
983-
* onChange(changes:{[idx: string, PropertyUpdate]}) {
984-
* // This will get called after any of the properties have been updated.
985-
* if (changes['propA']) {
986-
* // if propA was updated
987-
* }
988-
* if (changes['propA']) {
989-
* // if propB was updated
990-
* }
991-
* }
992-
* }
993-
* ```
994-
*/
995-
export const onChange: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onChange"));
958+
/**
959+
* Notify a directive when any of its bindings have changed.
960+
*
961+
* This method is called right after the directive's bindings have been checked,
962+
* and before any of its children's bindings have been checked.
963+
*
964+
* It is invoked only if at least one of the directive's bindings has changed.
965+
*
966+
* ## Example:
967+
*
968+
* ```
969+
* @Directive({
970+
* selector: '[class-set]',
971+
* properties: [
972+
* 'propA',
973+
* 'propB'
974+
* ],
975+
* lifecycle: [LifecycleEvent.onChange]
976+
* })
977+
* class ClassSet {
978+
* propA;
979+
* propB;
980+
* onChange(changes:{[idx: string, PropertyUpdate]}) {
981+
* // This will get called after any of the properties have been updated.
982+
* if (changes['propA']) {
983+
* // if propA was updated
984+
* }
985+
* if (changes['propA']) {
986+
* // if propB was updated
987+
* }
988+
* }
989+
* }
990+
* ```
991+
* @exportedAs angular2/annotations
992+
*/
993+
onChange,
996994

997-
/**
998-
* Notify a directive when it has been checked.
999-
*
1000-
* This method is called right after the directive's bindings have been checked,
1001-
* and before any of its children's bindings have been checked.
1002-
*
1003-
* It is invoked every time even when none of the directive's bindings has changed.
1004-
*
1005-
* ## Example:
1006-
*
1007-
* ```
1008-
* @Directive({
1009-
* selector: '[class-set]',
1010-
* lifecycle: [onCheck]
1011-
* })
1012-
* class ClassSet {
1013-
* onCheck() {
1014-
* }
1015-
* }
1016-
* ```
1017-
*/
1018-
export const onCheck: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onCheck"));
995+
/**
996+
* Notify a directive when it has been checked.
997+
*
998+
* This method is called right after the directive's bindings have been checked,
999+
* and before any of its children's bindings have been checked.
1000+
*
1001+
* It is invoked every time even when none of the directive's bindings has changed.
1002+
*
1003+
* ## Example:
1004+
*
1005+
* ```
1006+
* @Directive({
1007+
* selector: '[class-set]',
1008+
* lifecycle: [LifecycleEvent.onCheck]
1009+
* })
1010+
* class ClassSet {
1011+
* onCheck() {
1012+
* }
1013+
* }
1014+
* ```
1015+
* @exportedAs angular2/annotations
1016+
*/
1017+
onCheck,
10191018

1020-
/**
1021-
* Notify a directive when it has been checked the first itme.
1022-
*
1023-
* This method is called right after the directive's bindings have been checked,
1024-
* and before any of its children's bindings have been checked.
1025-
*
1026-
* It is invoked only once.
1027-
*
1028-
* ## Example:
1029-
*
1030-
* ```
1031-
* @Directive({
1032-
* selector: '[class-set]',
1033-
* lifecycle: [onInit]
1034-
* })
1035-
* class ClassSet {
1036-
* onInit() {
1037-
* }
1038-
* }
1039-
* ```
1040-
*/
1041-
export const onInit: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onInit"));
1019+
/**
1020+
* Notify a directive when it has been checked the first itme.
1021+
*
1022+
* This method is called right after the directive's bindings have been checked,
1023+
* and before any of its children's bindings have been checked.
1024+
*
1025+
* It is invoked only once.
1026+
*
1027+
* ## Example:
1028+
*
1029+
* ```
1030+
* @Directive({
1031+
* selector: '[class-set]',
1032+
* lifecycle: [LifecycleEvent.onInit]
1033+
* })
1034+
* class ClassSet {
1035+
* onInit() {
1036+
* }
1037+
* }
1038+
* ```
1039+
* @exportedAs angular2/annotations
1040+
*/
1041+
onInit,
10421042

1043-
/**
1044-
* Notify a directive when the bindings of all its children have been checked (whether they have
1045-
* changed or not).
1046-
*
1047-
* ## Example:
1048-
*
1049-
* ```
1050-
* @Directive({
1051-
* selector: '[class-set]',
1052-
* lifecycle: [onAllChangesDone]
1053-
* })
1054-
* class ClassSet {
1055-
*
1056-
* onAllChangesDone() {
1057-
* }
1058-
*
1059-
* }
1060-
* ```
1061-
*/
1062-
export const onAllChangesDone: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onAllChangesDone"));
1043+
/**
1044+
* Notify a directive when the bindings of all its children have been checked (whether they have
1045+
* changed or not).
1046+
*
1047+
* ## Example:
1048+
*
1049+
* ```
1050+
* @Directive({
1051+
* selector: '[class-set]',
1052+
* lifecycle: [LifecycleEvent.onAllChangesDone]
1053+
* })
1054+
* class ClassSet {
1055+
*
1056+
* onAllChangesDone() {
1057+
* }
1058+
*
1059+
* }
1060+
* ```
1061+
* @exportedAs angular2/annotations
1062+
*/
1063+
onAllChangesDone
1064+
}

modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ bool hasLifecycleHook(LifecycleEvent e, type, Directive annotation) {
1111
final List interfaces = reflector.interfaces(type);
1212
var interface;
1313

14-
if (e == onChange) {
14+
if (e == LifecycleEvent.onChange) {
1515
interface = OnChange;
16-
} else if (e == onDestroy) {
16+
} else if (e == LifecycleEvent.onDestroy) {
1717
interface = OnDestroy;
18-
} else if (e == onAllChangesDone) {
18+
} else if (e == LifecycleEvent.onAllChangesDone) {
1919
interface = OnAllChangesDone;
20-
} else if (e == onCheck) {
20+
} else if (e == LifecycleEvent.onCheck) {
2121
interface = OnCheck;
22-
} else if (e == onInit) {
22+
} else if (e == LifecycleEvent.onInit) {
2323
interface = OnInit;
2424
}
2525

modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ export function hasLifecycleHook(e: LifecycleEvent, type, annotation: Directive)
66
return annotation.lifecycle.indexOf(e) !== -1;
77
} else {
88
if (!(type instanceof Type)) return false;
9-
return e.name in(<any>type).prototype;
9+
var proto = (<any>type).prototype;
10+
switch (e) {
11+
case LifecycleEvent.onAllChangesDone:
12+
return !!proto.onAllChangesDone;
13+
case LifecycleEvent.onChange:
14+
return !!proto.onChange;
15+
case LifecycleEvent.onCheck:
16+
return !!proto.onCheck;
17+
case LifecycleEvent.onDestroy:
18+
return !!proto.onDestroy;
19+
case LifecycleEvent.onInit:
20+
return !!proto.onInit;
21+
default:
22+
return false;
23+
}
1024
}
11-
}
25+
}

modules/angular2/src/core/compiler/element_injector.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,7 @@ import * as avmModule from './view_manager';
4141
import {ViewContainerRef} from './view_container_ref';
4242
import {ElementRef} from './element_ref';
4343
import {ProtoViewRef, ViewRef} from './view_ref';
44-
import {
45-
Directive,
46-
Component,
47-
onChange,
48-
onDestroy,
49-
onCheck,
50-
onInit,
51-
onAllChangesDone
52-
} from 'angular2/src/core/annotations_impl/annotations';
44+
import {Directive, Component, LifecycleEvent} from 'angular2/src/core/annotations_impl/annotations';
5345
import {hasLifecycleHook} from './directive_lifecycle_reflector';
5446
import {ChangeDetector, ChangeDetectorRef, Pipes} from 'angular2/change_detection';
5547
import {QueryList} from './query_list';
@@ -253,11 +245,11 @@ export class DirectiveBinding extends ResolvedBinding {
253245
properties: ann.properties,
254246
readAttributes: DirectiveBinding._readAttributes(deps),
255247

256-
callOnDestroy: hasLifecycleHook(onDestroy, rb.key.token, ann),
257-
callOnChange: hasLifecycleHook(onChange, rb.key.token, ann),
258-
callOnCheck: hasLifecycleHook(onCheck, rb.key.token, ann),
259-
callOnInit: hasLifecycleHook(onInit, rb.key.token, ann),
260-
callOnAllChangesDone: hasLifecycleHook(onAllChangesDone, rb.key.token, ann),
248+
callOnDestroy: hasLifecycleHook(LifecycleEvent.onDestroy, rb.key.token, ann),
249+
callOnChange: hasLifecycleHook(LifecycleEvent.onChange, rb.key.token, ann),
250+
callOnCheck: hasLifecycleHook(LifecycleEvent.onCheck, rb.key.token, ann),
251+
callOnInit: hasLifecycleHook(LifecycleEvent.onInit, rb.key.token, ann),
252+
callOnAllChangesDone: hasLifecycleHook(LifecycleEvent.onAllChangesDone, rb.key.token, ann),
261253

262254
changeDetection: ann instanceof Component ? ann.changeDetection : null,
263255

modules/angular2/src/directives/class.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Directive, onCheck} from 'angular2/annotations';
1+
import {Directive, LifecycleEvent} from 'angular2/annotations';
22
import {ElementRef} from 'angular2/core';
33
import {Pipes} from 'angular2/src/change_detection/pipes/pipes';
44
import {Pipe} from 'angular2/src/change_detection/pipes/pipe';
@@ -28,7 +28,8 @@ import {ListWrapper, StringMapWrapper, isListLikeIterable} from 'angular2/src/fa
2828
* </div>
2929
* ```
3030
*/
31-
@Directive({selector: '[class]', lifecycle: [onCheck], properties: ['rawClass: class']})
31+
@Directive(
32+
{selector: '[class]', lifecycle: [LifecycleEvent.onCheck], properties: ['rawClass: class']})
3233
export class CSSClass {
3334
_pipe: Pipe;
3435
_rawClass;

modules/angular2/src/directives/ng_for.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import {Directive} from 'angular2/annotations';
2-
import {ViewContainerRef, ViewRef, ProtoViewRef, Pipes, onCheck, Pipe} from 'angular2/angular2';
2+
import {
3+
ViewContainerRef,
4+
ViewRef,
5+
ProtoViewRef,
6+
Pipes,
7+
LifecycleEvent,
8+
Pipe
9+
} from 'angular2/angular2';
310
import {isPresent, isBlank} from 'angular2/src/facade/lang';
411

512
/**
@@ -32,7 +39,8 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
3239
* - `<li template="ng-for #item of items; #i = index">...</li>`
3340
* - `<template ng-for #item [ng-for-of]="items" #i="index"><li>...</li></template>`
3441
*/
35-
@Directive({selector: '[ng-for][ng-for-of]', properties: ['ngForOf'], lifecycle: [onCheck]})
42+
@Directive(
43+
{selector: '[ng-for][ng-for-of]', properties: ['ngForOf'], lifecycle: [LifecycleEvent.onCheck]})
3644
export class NgFor {
3745
_ngForOf: any;
3846
_pipe: Pipe;

0 commit comments

Comments
 (0)