@@ -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
0 commit comments