11import { Template , Component , Decorator , NgElement , Ancestor , onChange } from 'angular2/core' ;
2+ import { Optional } from 'angular2/di' ;
23import { DOM } from 'angular2/src/dom/dom_adapter' ;
3- import { isBlank , isPresent , CONST } from 'angular2/src/facade/lang' ;
4+ import { isBlank , isPresent , isString , CONST } from 'angular2/src/facade/lang' ;
45import { StringMapWrapper , ListWrapper } from 'angular2/src/facade/collection' ;
56import { ControlGroup , Control } from './model' ;
67import * as validators from './validators' ;
@@ -64,7 +65,7 @@ function controlValueAccessorFor(controlType:string):ControlValueAccessor {
6465 }
6566} )
6667export class ControlDirective {
67- _groupDecorator :ControlGroupDirective ;
68+ _groupDirective :ControlGroupDirective ;
6869 _el :NgElement ;
6970
7071 controlName :string ;
@@ -73,8 +74,8 @@ export class ControlDirective {
7374
7475 validator :Function ;
7576
76- constructor ( @Ancestor ( ) groupDecorator :ControlGroupDirective , el :NgElement ) {
77- this . _groupDecorator = groupDecorator ;
77+ constructor ( @Ancestor ( ) groupDirective :ControlGroupDirective , el :NgElement ) {
78+ this . _groupDirective = groupDirective ;
7879 this . _el = el ;
7980 this . validator = validators . nullValidator ;
8081 }
@@ -86,7 +87,7 @@ export class ControlDirective {
8687 }
8788
8889 _initialize ( ) {
89- this . _groupDecorator . addDirective ( this ) ;
90+ this . _groupDirective . addDirective ( this ) ;
9091
9192 var c = this . _control ( ) ;
9293 c . validator = validators . compose ( [ c . validator , this . validator ] ) ;
@@ -108,7 +109,7 @@ export class ControlDirective {
108109 }
109110
110111 _control ( ) {
111- return this . _groupDecorator . findControl ( this . controlName ) ;
112+ return this . _groupDirective . findControl ( this . controlName ) ;
112113 }
113114}
114115
@@ -119,25 +120,45 @@ export class ControlDirective {
119120 }
120121} )
121122export class ControlGroupDirective {
123+ _groupDirective :ControlGroupDirective ;
124+ _controlGroupName :string ;
125+
122126 _controlGroup :ControlGroup ;
123127 _directives :List < ControlDirective > ;
124128
125- constructor ( ) {
129+ constructor ( @ Optional ( ) @ Ancestor ( ) groupDirective : ControlGroupDirective ) {
126130 super ( ) ;
131+ this . _groupDirective = groupDirective ;
127132 this . _directives = ListWrapper . create ( ) ;
128133 }
129134
130- set controlGroup ( controlGroup :ControlGroup ) {
131- this . _controlGroup = controlGroup ;
135+ set controlGroup ( controlGroup ) {
136+ if ( isString ( controlGroup ) ) {
137+ this . _controlGroupName = controlGroup ;
138+ } else {
139+ this . _controlGroup = controlGroup ;
140+ }
141+ this . _updateDomValue ( ) ;
142+ }
143+
144+ _updateDomValue ( ) {
132145 ListWrapper . forEach ( this . _directives , ( cd ) => cd . _updateDomValue ( ) ) ;
133146 }
134147
135148 addDirective ( c :ControlDirective ) {
136149 ListWrapper . push ( this . _directives , c ) ;
137150 }
138151
139- findControl ( name :string ) :Control {
140- return this . _controlGroup . controls [ name ] ;
152+ findControl ( name :string ) :any {
153+ return this . _getControlGroup ( ) . controls [ name ] ;
154+ }
155+
156+ _getControlGroup ( ) :ControlGroup {
157+ if ( isPresent ( this . _controlGroupName ) ) {
158+ return this . _groupDirective . findControl ( this . _controlGroupName )
159+ } else {
160+ return this . _controlGroup ;
161+ }
141162 }
142163}
143164
0 commit comments