Skip to content

Commit 8a10ede

Browse files
committed
feat(forms): added pristine and dirty
1 parent 38b96ed commit 8a10ede

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

modules/angular2/src/forms/model.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const INVALID = "INVALID";
1111
// get status():string;
1212
// get valid():boolean;
1313
// get errors():Map;
14+
// get pristine():boolean;
15+
// get dirty():boolean;
1416
// updateValue(value:any){}
1517
// setParent(parent){}
1618
//}
@@ -19,13 +21,15 @@ export class AbstractControl {
1921
_value:any;
2022
_status:string;
2123
_errors;
22-
_dirty:boolean;
24+
_updateNeeded:boolean;
25+
_pristine:boolean;
2326
_parent:ControlGroup;
2427
validator:Function;
2528

2629
constructor(validator:Function = nullValidator) {
2730
this.validator = validator;
28-
this._dirty = true;
31+
this._updateNeeded = true;
32+
this._pristine = true;
2933
}
3034

3135
get value() {
@@ -48,6 +52,15 @@ export class AbstractControl {
4852
return this._errors;
4953
}
5054

55+
get pristine() {
56+
this._updateIfNeeded();
57+
return this._pristine;
58+
}
59+
60+
get dirty() {
61+
return ! this.pristine;
62+
}
63+
5164
setParent(parent){
5265
this._parent = parent;
5366
}
@@ -70,13 +83,14 @@ export class Control extends AbstractControl {
7083

7184
updateValue(value:any) {
7285
this._value = value;
73-
this._dirty = true;
86+
this._updateNeeded = true;
87+
this._pristine = false;
7488
this._updateParent();
7589
}
7690

7791
_updateIfNeeded() {
78-
if (this._dirty) {
79-
this._dirty = false;
92+
if (this._updateNeeded) {
93+
this._updateNeeded = false;
8094
this._errors = this.validator(this);
8195
this._status = isPresent(this._errors) ? INVALID : VALID;
8296
}
@@ -95,12 +109,12 @@ export class ControlGroup extends AbstractControl {
95109
}
96110

97111
include(controlName:string) {
98-
this._dirty = true;
112+
this._updateNeeded = true;
99113
StringMapWrapper.set(this.optionals, controlName, true);
100114
}
101115

102116
exclude(controlName:string) {
103-
this._dirty = true;
117+
this._updateNeeded = true;
104118
StringMapWrapper.set(this.optionals, controlName, false);
105119
}
106120

@@ -116,26 +130,39 @@ export class ControlGroup extends AbstractControl {
116130
}
117131

118132
_updateIfNeeded() {
119-
if (this._dirty) {
120-
this._dirty = false;
133+
if (this._updateNeeded) {
134+
this._updateNeeded = false;
121135
this._value = this._reduceValue();
136+
this._pristine = this._reducePristine();
122137
this._errors = this.validator(this);
123138
this._status = isPresent(this._errors) ? INVALID : VALID;
124139
}
125140
}
126141

127142
_reduceValue() {
128-
var newValue = {};
143+
return this._reduceChildren({}, (acc, control, name) => {
144+
acc[name] = control.value;
145+
return acc;
146+
});
147+
}
148+
149+
_reducePristine() {
150+
return this._reduceChildren(true,
151+
(acc, control, name) => acc && control.pristine);
152+
}
153+
154+
_reduceChildren(initValue, fn:Function) {
155+
var res = initValue;
129156
StringMapWrapper.forEach(this.controls, (control, name) => {
130157
if (this._included(name)) {
131-
newValue[name] = control.value;
158+
res = fn(res, control, name);
132159
}
133160
});
134-
return newValue;
161+
return res;
135162
}
136163

137164
_controlChanged() {
138-
this._dirty = true;
165+
this._updateNeeded = true;
139166
this._updateParent();
140167
}
141168

modules/angular2/test/forms/model_spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ export function main() {
2222
expect(c.errors).toEqual({"required" : true});
2323
});
2424
});
25+
26+
describe("pristine", () => {
27+
it("should be true after creating a control", () => {
28+
var c = new Control("value");
29+
expect(c.pristine).toEqual(true);
30+
});
31+
32+
it("should be false after changing the value of the control", () => {
33+
var c = new Control("value");
34+
c.updateValue("new value");
35+
expect(c.pristine).toEqual(false);
36+
});
37+
});
38+
39+
describe("dirty", () => {
40+
it("should be false after creating a control", () => {
41+
var c = new Control("value");
42+
expect(c.dirty).toEqual(false);
43+
});
44+
45+
it("should be true after changing the value of the control", () => {
46+
var c = new Control("value");
47+
c.updateValue("new value");
48+
expect(c.dirty).toEqual(true);
49+
});
50+
});
2551
});
2652

2753
describe("ControlGroup", () => {
@@ -85,6 +111,23 @@ export function main() {
85111
});
86112
});
87113

114+
describe("pristine", () => {
115+
it("should be true after creating a control", () => {
116+
var c = new Control('value');
117+
var g = new ControlGroup({"one": c});
118+
119+
expect(g.pristine).toEqual(true);
120+
});
121+
122+
it("should be false after changing the value of the control", () => {
123+
var c = new Control('value');
124+
var g = new ControlGroup({"one": c});
125+
c.updateValue('new value');
126+
127+
expect(g.pristine).toEqual(false);
128+
});
129+
});
130+
88131
describe("optional components", () => {
89132
describe("contains", () => {
90133
var group;

0 commit comments

Comments
 (0)