Skip to content

Commit 96cadcc

Browse files
committed
refactor(forms): handle dirty/pristine explicitly
1 parent 31b6687 commit 96cadcc

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

modules/angular2/src/forms/directives/shared.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ export function setUpControl(c: Control, dir: ControlDirective) {
2323
dir.valueAccessor.registerOnChange(newValue => {
2424
dir.viewToModelUpdate(newValue);
2525
c.updateValue(newValue);
26+
c.markAsDirty();
2627
});
2728

2829
// model -> view
2930
c.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue));
3031

3132
// touched
32-
dir.valueAccessor.registerOnTouched(() => c.touch());
33+
dir.valueAccessor.registerOnTouched(() => c.markAsTouched());
3334
}
3435

3536
function _throwError(dir: ControlDirective, message: string): void {

modules/angular2/src/forms/model.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ export class AbstractControl {
6060

6161
get valueChanges(): Observable { return this._valueChanges; }
6262

63-
touch(): void { this._touched = true; }
63+
markAsTouched(): void { this._touched = true; }
64+
65+
markAsDirty({onlySelf}: {onlySelf?: boolean} = {}): void {
66+
onlySelf = isPresent(onlySelf) ? onlySelf : false;
67+
68+
this._pristine = false;
69+
if (isPresent(this._parent) && !onlySelf) {
70+
this._parent.markAsDirty({onlySelf: onlySelf});
71+
}
72+
}
6473

6574
setParent(parent) { this._parent = parent; }
6675

@@ -80,7 +89,6 @@ export class AbstractControl {
8089
emitEvent = isPresent(emitEvent) ? emitEvent : true;
8190

8291
this._updateValue();
83-
this._pristine = false;
8492

8593
if (emitEvent) {
8694
ObservableWrapper.callNext(this._valueChanges, this._value);

modules/angular2/test/forms/model_spec.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,6 @@ export function main() {
3939
});
4040
});
4141

42-
describe("pristine", () => {
43-
it("should be true after creating a control", () => {
44-
var c = new Control("value");
45-
expect(c.pristine).toEqual(true);
46-
});
47-
48-
it("should be false after changing the value of the control", () => {
49-
var c = new Control("value");
50-
c.updateValue("new value");
51-
expect(c.pristine).toEqual(false);
52-
});
53-
});
54-
5542
describe("dirty", () => {
5643
it("should be false after creating a control", () => {
5744
var c = new Control("value");
@@ -60,7 +47,7 @@ export function main() {
6047

6148
it("should be true after changing the value of the control", () => {
6249
var c = new Control("value");
63-
c.updateValue("new value");
50+
c.markAsDirty();
6451
expect(c.dirty).toEqual(true);
6552
});
6653
});
@@ -215,20 +202,22 @@ export function main() {
215202
});
216203
});
217204

218-
describe("pristine", () => {
219-
it("should be true after creating a control", () => {
220-
var c = new Control('value');
221-
var g = new ControlGroup({"one": c});
205+
describe("dirty", () => {
206+
var c,g;
222207

223-
expect(g.pristine).toEqual(true);
208+
beforeEach(() => {
209+
c = new Control('value');
210+
g = new ControlGroup({"one": c});
211+
});
212+
213+
it("should be false after creating a control", () => {
214+
expect(g.dirty).toEqual(false);
224215
});
225216

226217
it("should be false after changing the value of the control", () => {
227-
var c = new Control('value');
228-
var g = new ControlGroup({"one": c});
229-
c.updateValue('new value');
218+
c.markAsDirty();
230219

231-
expect(g.pristine).toEqual(false);
220+
expect(g.dirty).toEqual(true);
232221
});
233222
});
234223

@@ -452,19 +441,22 @@ export function main() {
452441
});
453442
});
454443

455-
describe("pristine", () => {
456-
it("should be true after creating a control", () => {
457-
var a = new ControlArray([new Control(1)]);
458-
expect(a.pristine).toBe(true);
444+
describe("dirty", () => {
445+
var c,a;
446+
447+
beforeEach(() => {
448+
c = new Control('value');
449+
a = new ControlArray([c]);
459450
});
460451

461-
it("should be false after changing the value of the control", () => {
462-
var c = new Control(1);
463-
var a = new ControlArray([c]);
452+
it("should be false after creating a control", () => {
453+
expect(a.dirty).toEqual(false);
454+
});
464455

465-
c.updateValue('new value');
456+
it("should be false after changing the value of the control", () => {
457+
c.markAsDirty();
466458

467-
expect(a.pristine).toEqual(false);
459+
expect(a.dirty).toEqual(true);
468460
});
469461
});
470462

0 commit comments

Comments
 (0)