feat(core/linker): add SimpleChanges type to lifecycle_hooks to simplify OnChanges signature

Closes #8557
This commit is contained in:
Ward Bell 2016-05-09 15:45:04 -07:00 committed by Misko Hevery
parent 6f3a6a55a0
commit 0a872ffd38
10 changed files with 27 additions and 14 deletions

View File

@ -1,7 +1,7 @@
import { import {
OnChanges, OnChanges,
OnDestroy, OnDestroy,
SimpleChange, SimpleChanges,
Directive, Directive,
forwardRef, forwardRef,
Host, Host,
@ -114,7 +114,7 @@ export class NgControlName extends NgControl implements OnChanges,
this.valueAccessor = selectValueAccessor(this, valueAccessors); this.valueAccessor = selectValueAccessor(this, valueAccessors);
} }
ngOnChanges(changes: {[key: string]: SimpleChange}) { ngOnChanges(changes: SimpleChanges) {
if (!this._added) { if (!this._added) {
this.formDirective.addControl(this); this.formDirective.addControl(this);
this._added = true; this._added = true;

View File

@ -1,6 +1,6 @@
import { import {
OnChanges, OnChanges,
SimpleChange, SimpleChanges,
Directive, Directive,
forwardRef, forwardRef,
Inject, Inject,
@ -100,7 +100,7 @@ export class NgFormControl extends NgControl implements OnChanges {
this.valueAccessor = selectValueAccessor(this, valueAccessors); this.valueAccessor = selectValueAccessor(this, valueAccessors);
} }
ngOnChanges(changes: {[key: string]: SimpleChange}): void { ngOnChanges(changes: SimpleChanges): void {
if (this._isControlChanged(changes)) { if (this._isControlChanged(changes)) {
setUpControl(this.form, this); setUpControl(this.form, this);
this.form.updateValueAndValidity({emitEvent: false}); this.form.updateValueAndValidity({emitEvent: false});

View File

@ -1,5 +1,5 @@
import { import {
SimpleChange, SimpleChanges,
OnChanges, OnChanges,
Directive, Directive,
forwardRef, forwardRef,
@ -116,7 +116,7 @@ export class NgFormModel extends ControlContainer implements Form,
super(); super();
} }
ngOnChanges(changes: {[key: string]: SimpleChange}): void { ngOnChanges(changes: SimpleChanges): void {
this._checkFormPresent(); this._checkFormPresent();
if (StringMapWrapper.contains(changes, "form")) { if (StringMapWrapper.contains(changes, "form")) {
var sync = composeValidators(this._validators); var sync = composeValidators(this._validators);

View File

@ -1,6 +1,6 @@
import { import {
OnChanges, OnChanges,
SimpleChange, SimpleChanges,
Directive, Directive,
forwardRef, forwardRef,
Inject, Inject,
@ -72,7 +72,7 @@ export class NgModel extends NgControl implements OnChanges {
this.valueAccessor = selectValueAccessor(this, valueAccessors); this.valueAccessor = selectValueAccessor(this, valueAccessors);
} }
ngOnChanges(changes: {[key: string]: SimpleChange}) { ngOnChanges(changes: SimpleChanges) {
if (!this._added) { if (!this._added) {
setUpControl(this._control, this); setUpControl(this._control, this);
this._control.updateValueAndValidity({emitEvent: false}); this._control.updateValueAndValidity({emitEvent: false});

View File

@ -28,7 +28,7 @@ import {
AfterContentChecked, AfterContentChecked,
AfterViewInit, AfterViewInit,
AfterViewChecked, AfterViewChecked,
SimpleChange, SimpleChanges,
provide provide
} from '@angular/core'; } from '@angular/core';
@ -141,7 +141,7 @@ class ComponentWithoutModuleId {
class ComponentWithEverything implements OnChanges, class ComponentWithEverything implements OnChanges,
OnInit, DoCheck, OnDestroy, AfterContentInit, AfterContentChecked, AfterViewInit, OnInit, DoCheck, OnDestroy, AfterContentInit, AfterContentChecked, AfterViewInit,
AfterViewChecked { AfterViewChecked {
ngOnChanges(changes: {[key: string]: SimpleChange}): void {} ngOnChanges(changes: SimpleChanges): void {}
ngOnInit(): void {} ngOnInit(): void {}
ngDoCheck(): void {} ngDoCheck(): void {}
ngOnDestroy(): void {} ngOnDestroy(): void {}

View File

@ -11,6 +11,7 @@ export {
WrappedValue, WrappedValue,
SimpleChange, SimpleChange,
SimpleChanges,
PipeTransform, PipeTransform,
DefaultIterableDiffer, DefaultIterableDiffer,
IterableDiffers, IterableDiffers,

View File

@ -41,6 +41,7 @@ export {
looseIdentical, looseIdentical,
uninitialized uninitialized
} from './change_detection_util'; } from './change_detection_util';
export {SimpleChanges} from '../metadata/lifecycle_hooks';
/** /**
* Structural diffing for `Object`s and `Map`s. * Structural diffing for `Object`s and `Map`s.

View File

@ -11,6 +11,15 @@ export enum LifecycleHooks {
AfterViewChecked AfterViewChecked
} }
/**
* A `changes` object whose keys are property names and
* values are instances of {@link SimpleChange}. See {@link OnChanges}
*/
export interface SimpleChanges {[propName: string]: SimpleChange};
/**
* @internal
*/
export var LIFECYCLE_HOOKS_VALUES = [ export var LIFECYCLE_HOOKS_VALUES = [
LifecycleHooks.OnInit, LifecycleHooks.OnInit,
LifecycleHooks.OnDestroy, LifecycleHooks.OnDestroy,
@ -53,7 +62,7 @@ export var LIFECYCLE_HOOKS_VALUES = [
* class MyComponent implements OnChanges { * class MyComponent implements OnChanges {
* @Input() myProp: any; * @Input() myProp: any;
* *
* ngOnChanges(changes: {[propName: string]: SimpleChange}) { * ngOnChanges(changes: SimpleChanges) {
* console.log('ngOnChanges - myProp = ' + changes['myProp'].currentValue); * console.log('ngOnChanges - myProp = ' + changes['myProp'].currentValue);
* } * }
* } * }
@ -72,7 +81,7 @@ export var LIFECYCLE_HOOKS_VALUES = [
* bootstrap(App).catch(err => console.error(err)); * bootstrap(App).catch(err => console.error(err));
* ``` * ```
*/ */
export abstract class OnChanges { abstract ngOnChanges(changes: {[key: string]: SimpleChange}); } export abstract class OnChanges { abstract ngOnChanges(changes: SimpleChanges); }
/** /**
* Implement this interface to execute custom initialization logic after your directive's * Implement this interface to execute custom initialization logic after your directive's

View File

@ -6,6 +6,7 @@ import {
ComponentFactory, ComponentFactory,
ComponentRef, ComponentRef,
SimpleChange, SimpleChange,
SimpleChanges,
ReflectiveInjector ReflectiveInjector
} from '@angular/core'; } from '@angular/core';
import {NG1_SCOPE} from './constants'; import {NG1_SCOPE} from './constants';
@ -19,7 +20,7 @@ const INITIAL_VALUE = {
export class DowngradeNg2ComponentAdapter { export class DowngradeNg2ComponentAdapter {
component: any = null; component: any = null;
inputChangeCount: number = 0; inputChangeCount: number = 0;
inputChanges: {[key: string]: SimpleChange} = null; inputChanges: SimpleChanges = null;
componentRef: ComponentRef<any> = null; componentRef: ComponentRef<any> = null;
changeDetector: ChangeDetectorRef = null; changeDetector: ChangeDetectorRef = null;
componentScope: angular.IScope; componentScope: angular.IScope;

View File

@ -7,6 +7,7 @@ import {
OnInit, OnInit,
OnChanges, OnChanges,
SimpleChange, SimpleChange,
SimpleChanges,
Type Type
} from '@angular/core'; } from '@angular/core';
import { import {
@ -248,7 +249,7 @@ class UpgradeNg1ComponentAdapter implements OnInit, OnChanges, DoCheck {
} }
} }
ngOnChanges(changes: {[name: string]: SimpleChange}) { ngOnChanges(changes: SimpleChanges) {
for (var name in changes) { for (var name in changes) {
if ((<Object>changes).hasOwnProperty(name)) { if ((<Object>changes).hasOwnProperty(name)) {
var change: SimpleChange = changes[name]; var change: SimpleChange = changes[name];