11import { isPresent } from 'angular2/src/facade/lang' ;
2+ import { Observable , ObservableWrapper } from 'angular2/src/facade/async' ;
23import { StringMap , StringMapWrapper } from 'angular2/src/facade/collection' ;
34import { Validators } from './validators' ;
45
@@ -21,39 +22,32 @@ export class AbstractControl {
2122 _value :any ;
2223 _status:string ;
2324 _errors ;
24- _updateNeeded:boolean ;
2525 _pristine:boolean ;
2626 _parent:ControlGroup ;
2727 validator:Function ;
2828
2929 constructor ( validator :Function ) {
3030 this . validator = validator ;
31- this . _updateNeeded = true ;
3231 this . _pristine = true ;
3332 }
3433
3534 get value ( ) {
36- this . _updateIfNeeded ( ) ;
3735 return this . _value ;
3836 }
3937
4038 get status ( ) {
41- this . _updateIfNeeded ( ) ;
4239 return this . _status ;
4340 }
4441
4542 get valid ( ) {
46- this . _updateIfNeeded ( ) ;
4743 return this . _status === VALID ;
4844 }
4945
5046 get errors ( ) {
51- this . _updateIfNeeded ( ) ;
5247 return this . _errors ;
5348 }
5449
5550 get pristine ( ) {
56- this . _updateIfNeeded ( ) ;
5751 return this . _pristine ;
5852 }
5953
@@ -65,57 +59,68 @@ export class AbstractControl {
6559 this . _parent = parent ;
6660 }
6761
68- _updateIfNeeded ( ) {
69- }
70-
7162 _updateParent ( ) {
7263 if ( isPresent ( this . _parent ) ) {
73- this . _parent . _controlChanged ( ) ;
64+ this . _parent . _updateValue ( ) ;
7465 }
7566 }
7667}
7768
7869export class Control extends AbstractControl {
70+ valueChanges :Observable ;
71+ _valueChangesController ;
72+
7973 constructor ( value :any , validator :Function = Validators . nullValidator ) {
8074 super ( validator ) ;
81- this . _value = value ;
75+ this . _setValueErrorsStatus ( value ) ;
76+
77+ this . _valueChangesController = ObservableWrapper . createController ( ) ;
78+ this . valueChanges = ObservableWrapper . createObservable ( this . _valueChangesController ) ;
8279 }
8380
8481 updateValue ( value :any ) {
85- this . _value = value ;
86- this . _updateNeeded = true ;
82+ this . _setValueErrorsStatus ( value ) ;
8783 this . _pristine = false ;
84+
85+ ObservableWrapper . callNext ( this . _valueChangesController , this . _value ) ;
86+
8887 this . _updateParent ( ) ;
8988 }
9089
91- _updateIfNeeded ( ) {
92- if ( this . _updateNeeded ) {
93- this . _updateNeeded = false ;
94- this . _errors = this . validator ( this ) ;
95- this . _status = isPresent ( this . _errors ) ? INVALID : VALID ;
96- }
90+ _setValueErrorsStatus ( value ) {
91+ this . _value = value ;
92+ this . _errors = this . validator ( this ) ;
93+ this . _status = isPresent ( this . _errors ) ? INVALID : VALID ;
9794 }
9895}
9996
10097export class ControlGroup extends AbstractControl {
10198 controls ;
10299 optionals ;
103100
101+ valueChanges :Observable ;
102+ _valueChangesController ;
103+
104104 constructor ( controls , optionals = null , validator :Function = Validators . group ) {
105105 super ( validator ) ;
106106 this . controls = controls ;
107107 this . optionals = isPresent ( optionals ) ? optionals : { } ;
108+
109+ this . _valueChangesController = ObservableWrapper . createController ( ) ;
110+ this . valueChanges = ObservableWrapper . createObservable ( this . _valueChangesController ) ;
111+
108112 this . _setParentForControls ( ) ;
113+ this . _setValueErrorsStatus ( ) ;
109114 }
110115
111116 include ( controlName :string ) {
112- this . _updateNeeded = true ;
113117 StringMapWrapper . set ( this . optionals , controlName , true ) ;
118+ this . _updateValue ( ) ;
114119 }
115120
116121 exclude ( controlName :string ) {
117- this . _updateNeeded = true ;
118122 StringMapWrapper . set ( this . optionals , controlName , false ) ;
123+ this . _updateValue ( ) ;
119124 }
120125
121126 contains ( controlName :string ) {
@@ -129,14 +134,19 @@ export class ControlGroup extends AbstractControl {
129134 } ) ;
130135 }
131136
132- _updateIfNeeded ( ) {
133- if ( this . _updateNeeded ) {
134- this . _updateNeeded = false ;
135- this . _value = this . _reduceValue ( ) ;
136- this . _pristine = this . _reducePristine ( ) ;
137- this . _errors = this . validator ( this ) ;
138- this . _status = isPresent ( this . _errors ) ? INVALID : VALID ;
139- }
137+ _updateValue ( ) {
138+ this . _setValueErrorsStatus ( ) ;
139+ this . _pristine = false ;
140+
141+ ObservableWrapper . callNext ( this . _valueChangesController , this . _value ) ;
142+
143+ this . _updateParent ( ) ;
144+ }
145+
146+ _setValueErrorsStatus ( ) {
147+ this . _value = this . _reduceValue ( ) ;
148+ this . _errors = this . validator ( this ) ;
149+ this . _status = isPresent ( this . _errors ) ? INVALID : VALID ;
140150 }
141151
142152 _reduceValue ( ) {
@@ -146,11 +156,6 @@ export class ControlGroup extends AbstractControl {
146156 } ) ;
147157 }
148158
149- _reducePristine ( ) {
150- return this . _reduceChildren ( true ,
151- ( acc , control , name ) => acc && control . pristine ) ;
152- }
153-
154159 _reduceChildren ( initValue , fn :Function ) {
155160 var res = initValue ;
156161 StringMapWrapper . forEach ( this . controls , ( control , name ) => {
@@ -161,11 +166,6 @@ export class ControlGroup extends AbstractControl {
161166 return res ;
162167 }
163168
164- _controlChanged ( ) {
165- this . _updateNeeded = true ;
166- this . _updateParent ( ) ;
167- }
168-
169169 _included ( controlName :string ) :boolean {
170170 var isOptional = StringMapWrapper . contains ( this . optionals , controlName ) ;
171171 return ! isOptional || StringMapWrapper . get ( this . optionals , controlName ) ;
0 commit comments