From 96cadcc29e1d39c774beb456bb036e877a7b9b0e Mon Sep 17 00:00:00 2001 From: vsavkin Date: Wed, 3 Jun 2015 11:54:39 -0700 Subject: [PATCH] refactor(forms): handle dirty/pristine explicitly --- .../angular2/src/forms/directives/shared.ts | 3 +- modules/angular2/src/forms/model.ts | 12 +++- modules/angular2/test/forms/model_spec.ts | 56 ++++++++----------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/modules/angular2/src/forms/directives/shared.ts b/modules/angular2/src/forms/directives/shared.ts index 2b4cf5401b..5ee2b68d9d 100644 --- a/modules/angular2/src/forms/directives/shared.ts +++ b/modules/angular2/src/forms/directives/shared.ts @@ -23,13 +23,14 @@ export function setUpControl(c: Control, dir: ControlDirective) { dir.valueAccessor.registerOnChange(newValue => { dir.viewToModelUpdate(newValue); c.updateValue(newValue); + c.markAsDirty(); }); // model -> view c.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue)); // touched - dir.valueAccessor.registerOnTouched(() => c.touch()); + dir.valueAccessor.registerOnTouched(() => c.markAsTouched()); } function _throwError(dir: ControlDirective, message: string): void { diff --git a/modules/angular2/src/forms/model.ts b/modules/angular2/src/forms/model.ts index f375c29a36..eb490b6679 100644 --- a/modules/angular2/src/forms/model.ts +++ b/modules/angular2/src/forms/model.ts @@ -60,7 +60,16 @@ export class AbstractControl { get valueChanges(): Observable { return this._valueChanges; } - touch(): void { this._touched = true; } + markAsTouched(): void { this._touched = true; } + + markAsDirty({onlySelf}: {onlySelf?: boolean} = {}): void { + onlySelf = isPresent(onlySelf) ? onlySelf : false; + + this._pristine = false; + if (isPresent(this._parent) && !onlySelf) { + this._parent.markAsDirty({onlySelf: onlySelf}); + } + } setParent(parent) { this._parent = parent; } @@ -80,7 +89,6 @@ export class AbstractControl { emitEvent = isPresent(emitEvent) ? emitEvent : true; this._updateValue(); - this._pristine = false; if (emitEvent) { ObservableWrapper.callNext(this._valueChanges, this._value); diff --git a/modules/angular2/test/forms/model_spec.ts b/modules/angular2/test/forms/model_spec.ts index 1bd89382b8..54b24ddf08 100644 --- a/modules/angular2/test/forms/model_spec.ts +++ b/modules/angular2/test/forms/model_spec.ts @@ -39,19 +39,6 @@ export function main() { }); }); - describe("pristine", () => { - it("should be true after creating a control", () => { - var c = new Control("value"); - expect(c.pristine).toEqual(true); - }); - - it("should be false after changing the value of the control", () => { - var c = new Control("value"); - c.updateValue("new value"); - expect(c.pristine).toEqual(false); - }); - }); - describe("dirty", () => { it("should be false after creating a control", () => { var c = new Control("value"); @@ -60,7 +47,7 @@ export function main() { it("should be true after changing the value of the control", () => { var c = new Control("value"); - c.updateValue("new value"); + c.markAsDirty(); expect(c.dirty).toEqual(true); }); }); @@ -215,20 +202,22 @@ export function main() { }); }); - describe("pristine", () => { - it("should be true after creating a control", () => { - var c = new Control('value'); - var g = new ControlGroup({"one": c}); + describe("dirty", () => { + var c,g; - expect(g.pristine).toEqual(true); + beforeEach(() => { + c = new Control('value'); + g = new ControlGroup({"one": c}); + }); + + it("should be false after creating a control", () => { + expect(g.dirty).toEqual(false); }); it("should be false after changing the value of the control", () => { - var c = new Control('value'); - var g = new ControlGroup({"one": c}); - c.updateValue('new value'); + c.markAsDirty(); - expect(g.pristine).toEqual(false); + expect(g.dirty).toEqual(true); }); }); @@ -452,19 +441,22 @@ export function main() { }); }); - describe("pristine", () => { - it("should be true after creating a control", () => { - var a = new ControlArray([new Control(1)]); - expect(a.pristine).toBe(true); + describe("dirty", () => { + var c,a; + + beforeEach(() => { + c = new Control('value'); + a = new ControlArray([c]); + }); + + it("should be false after creating a control", () => { + expect(a.dirty).toEqual(false); }); it("should be false after changing the value of the control", () => { - var c = new Control(1); - var a = new ControlArray([c]); + c.markAsDirty(); - c.updateValue('new value'); - - expect(a.pristine).toEqual(false); + expect(a.dirty).toEqual(true); }); });