1- import { Directive , onChange } from 'angular2/src/core/annotations_impl/annotations' ;
1+ import { Directive } from 'angular2/src/core/annotations_impl/annotations' ;
22import { Ancestor } from 'angular2/src/core/annotations_impl/visibility' ;
33import { ElementRef } from 'angular2/src/core/compiler/element_ref' ;
44import { Optional } from 'angular2/src/di/annotations_impl' ;
55import { Renderer } from 'angular2/src/render/api' ;
6- import { isPresent , isString , CONST_EXPR } from 'angular2/src/facade/lang' ;
6+ import { isPresent , isString , CONST_EXPR , isBlank , BaseException } from 'angular2/src/facade/lang' ;
77import { ListWrapper } from 'angular2/src/facade/collection' ;
8- import { ControlGroup } from './model' ;
8+ import { ControlGroup , Control , isControl } from './model' ;
99import { Validators } from './validators' ;
1010
1111//export interface ControlValueAccessor {
1212// writeValue(value):void{}
1313// set onChange(fn){}
1414//}
1515
16+ function _lookupControl ( groupDirective :ControlGroupDirective , controlOrName :any ) :any {
17+ if ( isControl ( controlOrName ) ) {
18+ return controlOrName ;
19+ }
20+
21+ if ( isBlank ( groupDirective ) ) {
22+ throw new BaseException ( `No control group found for "${ controlOrName } "` ) ;
23+ }
24+
25+ var control = groupDirective . findControl ( controlOrName ) ;
26+
27+ if ( isBlank ( control ) ) {
28+ throw new BaseException ( `Cannot find control "${ controlOrName } "` ) ;
29+ }
30+
31+ return control ;
32+ }
33+
1634/**
1735 * The default accessor for writing a value and listening to changes that is used by a {@link Control} directive.
1836 *
@@ -119,7 +137,6 @@ export class CheckboxControlValueAccessor {
119137 * @exportedAs angular2/forms
120138 */
121139@Directive ( {
122- lifecycle : [ onChange ] ,
123140 selector : '[control]' ,
124141 properties : {
125142 'controlOrName' : 'control'
@@ -128,25 +145,21 @@ export class CheckboxControlValueAccessor {
128145export class ControlDirective {
129146 _groupDirective :ControlGroupDirective ;
130147
131- controlOrName :any ;
148+ _controlOrName :any ;
132149 valueAccessor :any ; //ControlValueAccessor
133150
134151 validator :Function ;
135152
136153 constructor ( @Optional ( ) @Ancestor ( ) groupDirective :ControlGroupDirective , valueAccessor :DefaultValueAccessor ) {
137154 this . _groupDirective = groupDirective ;
138- this . controlOrName = null ;
155+ this . _controlOrName = null ;
139156 this . valueAccessor = valueAccessor ;
140157 this . validator = Validators . nullValidator ;
141158 }
142159
143- // TODO: vsavkin this should be moved into the constructor once static bindings
144- // are implemented
145- onChange ( _ ) {
146- this . _initialize ( ) ;
147- }
160+ set controlOrName ( controlOrName ) {
161+ this . _controlOrName = controlOrName ;
148162
149- _initialize ( ) {
150163 if ( isPresent ( this . _groupDirective ) ) {
151164 this . _groupDirective . addDirective ( this ) ;
152165 }
@@ -167,11 +180,7 @@ export class ControlDirective {
167180 }
168181
169182 _control ( ) {
170- if ( isString ( this . controlOrName ) ) {
171- return this . _groupDirective . findControl ( this . controlOrName ) ;
172- } else {
173- return this . controlOrName ;
174- }
183+ return _lookupControl ( this . _groupDirective , this . _controlOrName ) ;
175184 }
176185}
177186
@@ -218,27 +227,21 @@ export class ControlDirective {
218227@Directive ( {
219228 selector : '[control-group]' ,
220229 properties : {
221- 'controlGroup ' : 'control-group'
230+ 'controlOrName ' : 'control-group'
222231 }
223232} )
224233export class ControlGroupDirective {
225234 _groupDirective :ControlGroupDirective ;
226- _controlGroupName :string ;
227-
228- _controlGroup :ControlGroup ;
229235 _directives :List < ControlDirective > ;
236+ _controlOrName :any ;
230237
231238 constructor ( @Optional ( ) @Ancestor ( ) groupDirective :ControlGroupDirective ) {
232239 this . _groupDirective = groupDirective ;
233240 this . _directives = ListWrapper . create ( ) ;
234241 }
235242
236- set controlGroup ( controlGroup ) {
237- if ( isString ( controlGroup ) ) {
238- this . _controlGroupName = controlGroup ;
239- } else {
240- this . _controlGroup = controlGroup ;
241- }
243+ set controlOrName ( controlOrName ) {
244+ this . _controlOrName = controlOrName ;
242245 this . _updateDomValue ( ) ;
243246 }
244247
@@ -255,11 +258,7 @@ export class ControlGroupDirective {
255258 }
256259
257260 _getControlGroup ( ) :ControlGroup {
258- if ( isPresent ( this . _controlGroupName ) ) {
259- return this . _groupDirective . findControl ( this . _controlGroupName )
260- } else {
261- return this . _controlGroup ;
262- }
261+ return _lookupControl ( this . _groupDirective , this . _controlOrName ) ;
263262 }
264263}
265264
0 commit comments