From 0a872ffd387f59b18128dc31d7fa874766bc22af Mon Sep 17 00:00:00 2001 From: Ward Bell Date: Mon, 9 May 2016 15:45:04 -0700 Subject: [PATCH] feat(core/linker): add SimpleChanges type to lifecycle_hooks to simplify OnChanges signature Closes #8557 --- .../common/src/forms/directives/ng_control_name.ts | 4 ++-- .../common/src/forms/directives/ng_form_control.ts | 4 ++-- .../common/src/forms/directives/ng_form_model.ts | 4 ++-- .../common/src/forms/directives/ng_model.ts | 4 ++-- .../compiler/test/metadata_resolver_spec.ts | 4 ++-- modules/@angular/core/src/change_detection.ts | 1 + .../core/src/change_detection/change_detection.ts | 1 + .../@angular/core/src/metadata/lifecycle_hooks.ts | 13 +++++++++++-- .../@angular/upgrade/src/downgrade_ng2_adapter.ts | 3 ++- modules/@angular/upgrade/src/upgrade_ng1_adapter.ts | 3 ++- 10 files changed, 27 insertions(+), 14 deletions(-) diff --git a/modules/@angular/common/src/forms/directives/ng_control_name.ts b/modules/@angular/common/src/forms/directives/ng_control_name.ts index 93fa3f6058..19d2908734 100644 --- a/modules/@angular/common/src/forms/directives/ng_control_name.ts +++ b/modules/@angular/common/src/forms/directives/ng_control_name.ts @@ -1,7 +1,7 @@ import { OnChanges, OnDestroy, - SimpleChange, + SimpleChanges, Directive, forwardRef, Host, @@ -114,7 +114,7 @@ export class NgControlName extends NgControl implements OnChanges, this.valueAccessor = selectValueAccessor(this, valueAccessors); } - ngOnChanges(changes: {[key: string]: SimpleChange}) { + ngOnChanges(changes: SimpleChanges) { if (!this._added) { this.formDirective.addControl(this); this._added = true; diff --git a/modules/@angular/common/src/forms/directives/ng_form_control.ts b/modules/@angular/common/src/forms/directives/ng_form_control.ts index 0ec1598396..1389990992 100644 --- a/modules/@angular/common/src/forms/directives/ng_form_control.ts +++ b/modules/@angular/common/src/forms/directives/ng_form_control.ts @@ -1,6 +1,6 @@ import { OnChanges, - SimpleChange, + SimpleChanges, Directive, forwardRef, Inject, @@ -100,7 +100,7 @@ export class NgFormControl extends NgControl implements OnChanges { this.valueAccessor = selectValueAccessor(this, valueAccessors); } - ngOnChanges(changes: {[key: string]: SimpleChange}): void { + ngOnChanges(changes: SimpleChanges): void { if (this._isControlChanged(changes)) { setUpControl(this.form, this); this.form.updateValueAndValidity({emitEvent: false}); diff --git a/modules/@angular/common/src/forms/directives/ng_form_model.ts b/modules/@angular/common/src/forms/directives/ng_form_model.ts index 6924a85b90..8b654161e0 100644 --- a/modules/@angular/common/src/forms/directives/ng_form_model.ts +++ b/modules/@angular/common/src/forms/directives/ng_form_model.ts @@ -1,5 +1,5 @@ import { - SimpleChange, + SimpleChanges, OnChanges, Directive, forwardRef, @@ -116,7 +116,7 @@ export class NgFormModel extends ControlContainer implements Form, super(); } - ngOnChanges(changes: {[key: string]: SimpleChange}): void { + ngOnChanges(changes: SimpleChanges): void { this._checkFormPresent(); if (StringMapWrapper.contains(changes, "form")) { var sync = composeValidators(this._validators); diff --git a/modules/@angular/common/src/forms/directives/ng_model.ts b/modules/@angular/common/src/forms/directives/ng_model.ts index accf55a2ef..c36dd7dbea 100644 --- a/modules/@angular/common/src/forms/directives/ng_model.ts +++ b/modules/@angular/common/src/forms/directives/ng_model.ts @@ -1,6 +1,6 @@ import { OnChanges, - SimpleChange, + SimpleChanges, Directive, forwardRef, Inject, @@ -72,7 +72,7 @@ export class NgModel extends NgControl implements OnChanges { this.valueAccessor = selectValueAccessor(this, valueAccessors); } - ngOnChanges(changes: {[key: string]: SimpleChange}) { + ngOnChanges(changes: SimpleChanges) { if (!this._added) { setUpControl(this._control, this); this._control.updateValueAndValidity({emitEvent: false}); diff --git a/modules/@angular/compiler/test/metadata_resolver_spec.ts b/modules/@angular/compiler/test/metadata_resolver_spec.ts index abac4b9b16..02e2312d00 100644 --- a/modules/@angular/compiler/test/metadata_resolver_spec.ts +++ b/modules/@angular/compiler/test/metadata_resolver_spec.ts @@ -28,7 +28,7 @@ import { AfterContentChecked, AfterViewInit, AfterViewChecked, - SimpleChange, + SimpleChanges, provide } from '@angular/core'; @@ -141,7 +141,7 @@ class ComponentWithoutModuleId { class ComponentWithEverything implements OnChanges, OnInit, DoCheck, OnDestroy, AfterContentInit, AfterContentChecked, AfterViewInit, AfterViewChecked { - ngOnChanges(changes: {[key: string]: SimpleChange}): void {} + ngOnChanges(changes: SimpleChanges): void {} ngOnInit(): void {} ngDoCheck(): void {} ngOnDestroy(): void {} diff --git a/modules/@angular/core/src/change_detection.ts b/modules/@angular/core/src/change_detection.ts index f67ef4e4d5..90f99e2cca 100644 --- a/modules/@angular/core/src/change_detection.ts +++ b/modules/@angular/core/src/change_detection.ts @@ -11,6 +11,7 @@ export { WrappedValue, SimpleChange, + SimpleChanges, PipeTransform, DefaultIterableDiffer, IterableDiffers, diff --git a/modules/@angular/core/src/change_detection/change_detection.ts b/modules/@angular/core/src/change_detection/change_detection.ts index cdd341b6e2..9377fdb098 100644 --- a/modules/@angular/core/src/change_detection/change_detection.ts +++ b/modules/@angular/core/src/change_detection/change_detection.ts @@ -41,6 +41,7 @@ export { looseIdentical, uninitialized } from './change_detection_util'; +export {SimpleChanges} from '../metadata/lifecycle_hooks'; /** * Structural diffing for `Object`s and `Map`s. diff --git a/modules/@angular/core/src/metadata/lifecycle_hooks.ts b/modules/@angular/core/src/metadata/lifecycle_hooks.ts index 0c4d384142..1b1ad99699 100644 --- a/modules/@angular/core/src/metadata/lifecycle_hooks.ts +++ b/modules/@angular/core/src/metadata/lifecycle_hooks.ts @@ -11,6 +11,15 @@ export enum LifecycleHooks { 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 = [ LifecycleHooks.OnInit, LifecycleHooks.OnDestroy, @@ -53,7 +62,7 @@ export var LIFECYCLE_HOOKS_VALUES = [ * class MyComponent implements OnChanges { * @Input() myProp: any; * - * ngOnChanges(changes: {[propName: string]: SimpleChange}) { + * ngOnChanges(changes: SimpleChanges) { * console.log('ngOnChanges - myProp = ' + changes['myProp'].currentValue); * } * } @@ -72,7 +81,7 @@ export var LIFECYCLE_HOOKS_VALUES = [ * 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 diff --git a/modules/@angular/upgrade/src/downgrade_ng2_adapter.ts b/modules/@angular/upgrade/src/downgrade_ng2_adapter.ts index dfbf35bc9b..3e4502fcaa 100644 --- a/modules/@angular/upgrade/src/downgrade_ng2_adapter.ts +++ b/modules/@angular/upgrade/src/downgrade_ng2_adapter.ts @@ -6,6 +6,7 @@ import { ComponentFactory, ComponentRef, SimpleChange, + SimpleChanges, ReflectiveInjector } from '@angular/core'; import {NG1_SCOPE} from './constants'; @@ -19,7 +20,7 @@ const INITIAL_VALUE = { export class DowngradeNg2ComponentAdapter { component: any = null; inputChangeCount: number = 0; - inputChanges: {[key: string]: SimpleChange} = null; + inputChanges: SimpleChanges = null; componentRef: ComponentRef = null; changeDetector: ChangeDetectorRef = null; componentScope: angular.IScope; diff --git a/modules/@angular/upgrade/src/upgrade_ng1_adapter.ts b/modules/@angular/upgrade/src/upgrade_ng1_adapter.ts index a45768fe7e..2935ae49aa 100644 --- a/modules/@angular/upgrade/src/upgrade_ng1_adapter.ts +++ b/modules/@angular/upgrade/src/upgrade_ng1_adapter.ts @@ -7,6 +7,7 @@ import { OnInit, OnChanges, SimpleChange, + SimpleChanges, Type } from '@angular/core'; import { @@ -248,7 +249,7 @@ class UpgradeNg1ComponentAdapter implements OnInit, OnChanges, DoCheck { } } - ngOnChanges(changes: {[name: string]: SimpleChange}) { + ngOnChanges(changes: SimpleChanges) { for (var name in changes) { if ((changes).hasOwnProperty(name)) { var change: SimpleChange = changes[name];