feat(core): added support for detecting lifecycle events based on interfaces

This commit is contained in:
vsavkin
2015-05-27 08:08:14 -07:00
parent 2b6a653050
commit 30b6542fc8
12 changed files with 257 additions and 60 deletions

View File

@ -0,0 +1,26 @@
import 'package:angular2/src/core/annotations_impl/annotations.dart';
import 'package:angular2/src/core/compiler/interfaces.dart';
import 'package:angular2/src/reflection/reflection.dart';
bool hasLifecycleHook(LifecycleEvent e, type, Directive annotation) {
if (annotation.lifecycle != null) {
return annotation.lifecycle.contains(e);
} else {
if (type is! Type) return false;
final List interfaces = reflector.interfaces(type);
var interface;
if (e == onChange) {
interface = OnChange;
} else if (e == onDestroy) {
interface = OnDestroy;
} else if (e == onAllChangesDone) {
interface = OnAllChangesDone;
}
return interfaces.contains(interface);
}
}

View File

@ -0,0 +1,11 @@
import {Type, isPresent} from 'angular2/src/facade/lang';
import {LifecycleEvent, Directive} from 'angular2/src/core/annotations_impl/annotations';
export function hasLifecycleHook(e: LifecycleEvent, type, annotation: Directive): boolean {
if (isPresent(annotation.lifecycle)) {
return annotation.lifecycle.indexOf(e) !== -1;
} else {
if (!(type instanceof Type)) return false;
return e.name in(<any>type).prototype;
}
}

View File

@ -28,6 +28,7 @@ import {
onDestroy,
onAllChangesDone
} from 'angular2/src/core/annotations_impl/annotations';
import {hasLifecycleHook} from './directive_lifecycle_reflector';
import {ChangeDetector, ChangeDetectorRef} from 'angular2/change_detection';
import {QueryList} from './query_list';
import {reflector} from 'angular2/src/reflection/reflection';
@ -282,7 +283,6 @@ export class DirectiveBinding extends ResolvedBinding {
var resolvedViewInjectables = ann instanceof Component && isPresent(ann.viewInjector) ?
resolveBindings(ann.viewInjector) :
[];
var metadata = new DirectiveMetadata({
id: stringify(rb.key.token),
type: ann instanceof
@ -300,9 +300,11 @@ export class DirectiveBinding extends ResolvedBinding {
null,
properties: isPresent(ann.properties) ? MapWrapper.createFromStringMap(ann.properties) : null,
readAttributes: DirectiveBinding._readAttributes(deps),
callOnDestroy: ann.hasLifecycleHook(onDestroy),
callOnChange: ann.hasLifecycleHook(onChange),
callOnAllChangesDone: ann.hasLifecycleHook(onAllChangesDone),
callOnDestroy: hasLifecycleHook(onDestroy, rb.key.token, ann),
callOnChange: hasLifecycleHook(onChange, rb.key.token, ann),
callOnAllChangesDone: hasLifecycleHook(onAllChangesDone, rb.key.token, ann),
changeDetection: ann instanceof
Component ? ann.changeDetection : null
});

View File

@ -12,6 +12,7 @@ export interface OnChange { onChange(changes: StringMap<string, any>): void; }
export interface OnDestroy { onDestroy(): void; }
/**
* Defines lifecycle method [onAllChangesDone ] called when the bindings of all its children have been changed.
* Defines lifecycle method [onAllChangesDone ] called when the bindings of all its children have
* been changed.
*/
export interface OnAllChangesDone { onAllChangesDone (): void; }
export interface OnAllChangesDone { onAllChangesDone(): void; }