feat(change_detection): added onInit and onCheck hooks
This commit is contained in:
@ -19,6 +19,12 @@ bool hasLifecycleHook(LifecycleEvent e, type, Directive annotation) {
|
||||
|
||||
} else if (e == onAllChangesDone) {
|
||||
interface = OnAllChangesDone;
|
||||
|
||||
} else if (e == onCheck) {
|
||||
interface = OnCheck;
|
||||
|
||||
} else if (e == onInit) {
|
||||
interface = OnInit;
|
||||
}
|
||||
|
||||
return interfaces.contains(interface);
|
||||
|
@ -26,6 +26,8 @@ import {
|
||||
Component,
|
||||
onChange,
|
||||
onDestroy,
|
||||
onCheck,
|
||||
onInit,
|
||||
onAllChangesDone
|
||||
} from 'angular2/src/core/annotations_impl/annotations';
|
||||
import {hasLifecycleHook} from './directive_lifecycle_reflector';
|
||||
@ -303,6 +305,8 @@ export class DirectiveBinding extends ResolvedBinding {
|
||||
|
||||
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),
|
||||
|
||||
changeDetection: ann instanceof
|
||||
|
@ -11,6 +11,16 @@ export interface OnChange { onChange(changes: StringMap<string, any>): void; }
|
||||
*/
|
||||
export interface OnDestroy { onDestroy(): void; }
|
||||
|
||||
/**
|
||||
* Defines lifecycle method [onCheck] called when a directive is being checked.
|
||||
*/
|
||||
export interface OnCheck { onCheck(): void; }
|
||||
|
||||
/**
|
||||
* Defines lifecycle method [onInit] called when a directive is being checked the first time.
|
||||
*/
|
||||
export interface OnInit { onInit(): void; }
|
||||
|
||||
/**
|
||||
* Defines lifecycle method [onAllChangesDone ] called when the bindings of all its children have
|
||||
* been changed.
|
||||
|
@ -85,17 +85,27 @@ class BindingRecordsCreator {
|
||||
for (var i = 0; i < directiveBinders.length; i++) {
|
||||
var directiveBinder = directiveBinders[i];
|
||||
var directiveMetadata = allDirectiveMetadatas[directiveBinder.directiveIndex];
|
||||
var directiveRecord = this._getDirectiveRecord(boundElementIndex, i, directiveMetadata);
|
||||
|
||||
// directive properties
|
||||
MapWrapper.forEach(directiveBinder.propertyBindings, (astWithSource, propertyName) => {
|
||||
// TODO: these setters should eventually be created by change detection, to make
|
||||
// it monomorphic!
|
||||
var setter = reflector.setter(propertyName);
|
||||
var directiveRecord = this._getDirectiveRecord(boundElementIndex, i, directiveMetadata);
|
||||
ListWrapper.push(bindings, BindingRecord.createForDirective(astWithSource, propertyName,
|
||||
setter, directiveRecord));
|
||||
});
|
||||
|
||||
if (directiveRecord.callOnChange) {
|
||||
ListWrapper.push(bindings, BindingRecord.createDirectiveOnChange(directiveRecord));
|
||||
}
|
||||
if (directiveRecord.callOnInit) {
|
||||
ListWrapper.push(bindings, BindingRecord.createDirectiveOnInit(directiveRecord));
|
||||
}
|
||||
if (directiveRecord.callOnCheck) {
|
||||
ListWrapper.push(bindings, BindingRecord.createDirectiveOnCheck(directiveRecord));
|
||||
}
|
||||
|
||||
// host properties
|
||||
MapWrapper.forEach(directiveBinder.hostPropertyBindings, (astWithSource, propertyName) => {
|
||||
var dirIndex = new DirectiveIndex(boundElementIndex, i);
|
||||
@ -110,12 +120,14 @@ class BindingRecordsCreator {
|
||||
var id = boundElementIndex * 100 + directiveIndex;
|
||||
|
||||
if (!MapWrapper.contains(this._directiveRecordsMap, id)) {
|
||||
var changeDetection = directiveMetadata.changeDetection;
|
||||
|
||||
MapWrapper.set(this._directiveRecordsMap, id,
|
||||
new DirectiveRecord(new DirectiveIndex(boundElementIndex, directiveIndex),
|
||||
directiveMetadata.callOnAllChangesDone,
|
||||
directiveMetadata.callOnChange, changeDetection));
|
||||
MapWrapper.set(this._directiveRecordsMap, id, new DirectiveRecord({
|
||||
directiveIndex: new DirectiveIndex(boundElementIndex, directiveIndex),
|
||||
callOnAllChangesDone: directiveMetadata.callOnAllChangesDone,
|
||||
callOnChange: directiveMetadata.callOnChange,
|
||||
callOnCheck: directiveMetadata.callOnCheck,
|
||||
callOnInit: directiveMetadata.callOnInit,
|
||||
changeDetection: directiveMetadata.changeDetection
|
||||
}));
|
||||
}
|
||||
|
||||
return MapWrapper.get(this._directiveRecordsMap, id);
|
||||
|
Reference in New Issue
Block a user