Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions modules/angular2/annotations.ts
Expand Up @@ -13,12 +13,7 @@
export {
ComponentAnnotation,
DirectiveAnnotation,
LifecycleEvent,
onDestroy,
onChange,
onCheck,
onInit,
onAllChangesDone
LifecycleEvent
} from './src/core/annotations/annotations';

export {ViewAnnotation} from 'angular2/src/core/annotations/view';
Expand Down
7 changes: 1 addition & 6 deletions modules/angular2/src/core/annotations/annotations.ts
Expand Up @@ -6,10 +6,5 @@
export {
Component as ComponentAnnotation,
Directive as DirectiveAnnotation,
LifecycleEvent,
onDestroy,
onChange,
onCheck,
onInit,
onAllChangesDone
LifecycleEvent
} from '../annotations_impl/annotations';
246 changes: 124 additions & 122 deletions modules/angular2/src/core/annotations_impl/annotations.ts
Expand Up @@ -933,130 +933,132 @@ export class Component extends Directive {
* - `onCheck`,
* - `onAllChangesDone`
*/
@CONST()
export class LifecycleEvent {
constructor(public name: string) {}
}

/**
* 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"));
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: [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 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: [onCheck]
* })
* class ClassSet {
* onCheck() {
* }
* }
* ```
*/
export const onCheck: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onCheck"));
/**
* 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: [onInit]
* })
* class ClassSet {
* onInit() {
* }
* }
* ```
*/
export const onInit: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onInit"));
/**
* 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: [onAllChangesDone]
* })
* class ClassSet {
*
* onAllChangesDone() {
* }
*
* }
* ```
*/
export const onAllChangesDone: LifecycleEvent = CONST_EXPR(new LifecycleEvent("onAllChangesDone"));
/**
* 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
}
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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;
}
}
}
}
20 changes: 6 additions & 14 deletions modules/angular2/src/core/compiler/element_injector.ts
Expand Up @@ -42,15 +42,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';
Expand Down Expand Up @@ -254,11 +246,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,
Expand Down
5 changes: 3 additions & 2 deletions modules/angular2/src/directives/class.ts
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions modules/angular2/src/directives/ng_for.ts
@@ -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';

/**
Expand Down Expand Up @@ -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;
Expand Down