From 445e5922ecc2d86b7b444450d5934a6456e3675b Mon Sep 17 00:00:00 2001 From: Dzmitry Shylovich Date: Wed, 19 Oct 2016 19:55:50 +0300 Subject: [PATCH] feat(forms): make 'parent' a public property of 'AbstractControl' (#11855) --- modules/@angular/forms/src/model.ts | 30 +++++++++++++++---------- tools/public_api_guard/forms/index.d.ts | 1 + 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/modules/@angular/forms/src/model.ts b/modules/@angular/forms/src/model.ts index fccd697c48..a094da1aff 100644 --- a/modules/@angular/forms/src/model.ts +++ b/modules/@angular/forms/src/model.ts @@ -11,7 +11,7 @@ import {fromPromise} from 'rxjs/observable/fromPromise'; import {composeAsyncValidators, composeValidators} from './directives/shared'; import {AsyncValidatorFn, ValidatorFn} from './directives/validators'; import {EventEmitter, Observable} from './facade/async'; -import {ListWrapper, StringMapWrapper} from './facade/collection'; +import {StringMapWrapper} from './facade/collection'; import {isBlank, isPresent, isStringMap, normalizeBool} from './facade/lang'; import {isPromise} from './private_import_core'; @@ -48,7 +48,7 @@ function _find(control: AbstractControl, path: Array| string, del if (!(path instanceof Array)) { path = (path).split(delimiter); } - if (path instanceof Array && ListWrapper.isEmpty(path)) return null; + if (path instanceof Array && (path.length === 0)) return null; return (>path).reduce((v, name) => { if (v instanceof FormGroup) { @@ -109,6 +109,11 @@ export abstract class AbstractControl { */ get value(): any { return this._value; } + /** + * The parent control. + */ + get parent(): FormGroup|FormArray { return this._parent; } + /** * The validation status of the control. There are four possible * validation statuses: @@ -491,7 +496,7 @@ export abstract class AbstractControl { * If no path is given, it checks for the error on the present control. */ getError(errorCode: string, path: string[] = null): any { - var control = isPresent(path) && !ListWrapper.isEmpty(path) ? this.get(path) : this; + const control = isPresent(path) && (path.length > 0) ? this.get(path) : this; if (isPresent(control) && isPresent(control._errors)) { return control._errors[errorCode]; } else { @@ -564,7 +569,7 @@ export abstract class AbstractControl { /** @internal */ _anyControlsHaveStatus(status: string): boolean { - return this._anyControls((control: AbstractControl) => control.status == status); + return this._anyControls((control: AbstractControl) => control.status === status); } /** @internal */ @@ -1057,7 +1062,7 @@ export class FormGroup extends AbstractControl { } /** @internal */ - _setUpControls() { + _setUpControls(): void { this._forEachChild((control: AbstractControl) => { control.setParent(this); control._registerOnCollectionChange(this._onCollectionChange); @@ -1065,11 +1070,11 @@ export class FormGroup extends AbstractControl { } /** @internal */ - _updateValue() { this._value = this._reduceValue(); } + _updateValue(): void { this._value = this._reduceValue(); } /** @internal */ _anyControls(condition: Function): boolean { - var res = false; + let res = false; this._forEachChild((control: AbstractControl, name: string) => { res = res || (this.contains(name) && condition(control)); }); @@ -1089,7 +1094,7 @@ export class FormGroup extends AbstractControl { /** @internal */ _reduceChildren(initValue: any, fn: Function) { - var res = initValue; + let res = initValue; this._forEachChild( (control: AbstractControl, name: string) => { res = fn(res, control, name); }); return res; @@ -1188,7 +1193,8 @@ export class FormArray extends AbstractControl { * Insert a new {@link AbstractControl} at the given `index` in the array. */ insert(index: number, control: AbstractControl): void { - ListWrapper.insert(this.controls, index, control); + this.controls.splice(index, 0, control); + this._registerControl(control); this.updateValueAndValidity(); this._onCollectionChange(); @@ -1199,7 +1205,7 @@ export class FormArray extends AbstractControl { */ removeAt(index: number): void { if (this.controls[index]) this.controls[index]._registerOnCollectionChange(() => {}); - ListWrapper.removeAt(this.controls, index); + this.controls.splice(index, 1); this.updateValueAndValidity(); this._onCollectionChange(); } @@ -1209,10 +1215,10 @@ export class FormArray extends AbstractControl { */ setControl(index: number, control: AbstractControl): void { if (this.controls[index]) this.controls[index]._registerOnCollectionChange(() => {}); - ListWrapper.removeAt(this.controls, index); + this.controls.splice(index, 1); if (control) { - ListWrapper.insert(this.controls, index, control); + this.controls.splice(index, 0, control); this._registerControl(control); } diff --git a/tools/public_api_guard/forms/index.d.ts b/tools/public_api_guard/forms/index.d.ts index 6bbe96802c..7604a0cb6f 100644 --- a/tools/public_api_guard/forms/index.d.ts +++ b/tools/public_api_guard/forms/index.d.ts @@ -8,6 +8,7 @@ export declare abstract class AbstractControl { [key: string]: any; }; invalid: boolean; + parent: FormGroup | FormArray; pending: boolean; pristine: boolean; root: AbstractControl;