@@ -47,6 +47,7 @@ import {
4747} from 'angular2/src/core/change_detection/change_detection' ;
4848import { QueryList } from './query_list' ;
4949import { reflector } from 'angular2/src/core/reflection/reflection' ;
50+ import { SetterFn } from 'angular2/src/core/reflection/types' ;
5051import { RenderDirectiveMetadata } from 'angular2/src/core/render/api' ;
5152import { EventConfig } from 'angular2/src/core/render/event_config' ;
5253import { PipeBinding } from '../pipes/pipe_binding' ;
@@ -138,6 +139,17 @@ export class DirectiveBinding extends ResolvedBinding {
138139
139140 get callOnDestroy ( ) : boolean { return this . metadata . callOnDestroy ; }
140141
142+ get queries ( ) : QueryMetadataWithSetter [ ] {
143+ if ( isBlank ( this . metadata . queries ) ) return [ ] ;
144+
145+ var res = [ ] ;
146+ StringMapWrapper . forEach ( this . metadata . queries , ( meta , fieldName ) => {
147+ var setter = reflector . setter ( fieldName ) ;
148+ res . push ( new QueryMetadataWithSetter ( setter , meta ) ) ;
149+ } ) ;
150+ return res ;
151+ }
152+
141153 get eventEmitters ( ) : string [ ] {
142154 return isPresent ( this . metadata ) && isPresent ( this . metadata . events ) ? this . metadata . events : [ ] ;
143155 }
@@ -151,6 +163,7 @@ export class DirectiveBinding extends ResolvedBinding {
151163 var rf = rb . resolvedFactories [ 0 ] ;
152164 var deps = rf . dependencies . map ( DirectiveDependency . createFrom ) ;
153165 var token = binding . token ;
166+
154167 var metadata = RenderDirectiveMetadata . create ( {
155168 id : stringify ( binding . token ) ,
156169 type : meta instanceof ComponentMetadata ? RenderDirectiveMetadata . COMPONENT_TYPE :
@@ -161,6 +174,7 @@ export class DirectiveBinding extends ResolvedBinding {
161174 host : isPresent ( meta . host ) ? MapWrapper . createFromStringMap ( meta . host ) : null ,
162175 properties : meta . properties ,
163176 readAttributes : DirectiveBinding . _readAttributes ( < any > deps ) ,
177+ queries : meta . queries ,
164178
165179 callOnDestroy : hasLifecycleHook ( LifecycleHooks . OnDestroy , token ) ,
166180 callOnChanges : hasLifecycleHook ( LifecycleHooks . OnChanges , token ) ,
@@ -203,6 +217,10 @@ export class PreBuiltObjects {
203217 public elementRef : ElementRef , public templateRef : TemplateRef ) { }
204218}
205219
220+ export class QueryMetadataWithSetter {
221+ constructor ( public setter : SetterFn , public metadata : QueryMetadata ) { }
222+ }
223+
206224export class EventEmitterAccessor {
207225 constructor ( public eventName : string , public getter : Function ) { }
208226
@@ -214,17 +232,6 @@ export class EventEmitterAccessor {
214232 }
215233}
216234
217- export class HostActionAccessor {
218- constructor ( public methodName : string , public getter : Function ) { }
219-
220- subscribe ( view : viewModule . AppView , boundElementIndex : number , directive : Object ) : Object {
221- var eventEmitter = this . getter ( directive ) ;
222- return ObservableWrapper . subscribe < any [ ] > (
223- eventEmitter ,
224- actionArgs => view . invokeElementMethod ( boundElementIndex , this . methodName , actionArgs ) ) ;
225- }
226- }
227-
228235function _createEventEmitterAccessors ( bwv : BindingWithVisibility ) : EventEmitterAccessor [ ] {
229236 var binding = bwv . binding ;
230237 if ( ! ( binding instanceof DirectiveBinding ) ) return [ ] ;
@@ -554,19 +561,25 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
554561 for ( var i = 0 ; i < deps . length ; i ++ ) {
555562 var dep = deps [ i ] ;
556563 if ( isPresent ( dep . queryDecorator ) ) {
557- this . _createQueryRef ( dep . queryDecorator ) ;
564+ this . _createQueryRef ( null , null , dep . queryDecorator ) ;
558565 }
559566 }
560567 }
568+ _buildQueriesForDirective ( dirIndex : number , meta : QueryMetadataWithSetter [ ] ) : void {
569+ for ( var i = 0 ; i < meta . length ; i ++ ) {
570+ var m = meta [ i ] ;
571+ this . _createQueryRef ( dirIndex , m . setter , m . metadata ) ;
572+ }
573+ }
561574
562- private _createQueryRef ( query : QueryMetadata ) : void {
575+ private _createQueryRef ( dirIndex : number , setter : SetterFn , query : QueryMetadata ) : void {
563576 var queryList = new QueryList < any > ( ) ;
564577 if ( isBlank ( this . _query0 ) ) {
565- this . _query0 = new QueryRef ( query , queryList , this ) ;
578+ this . _query0 = new QueryRef ( dirIndex , setter , query , queryList , this ) ;
566579 } else if ( isBlank ( this . _query1 ) ) {
567- this . _query1 = new QueryRef ( query , queryList , this ) ;
580+ this . _query1 = new QueryRef ( dirIndex , setter , query , queryList , this ) ;
568581 } else if ( isBlank ( this . _query2 ) ) {
569- this . _query2 = new QueryRef ( query , queryList , this ) ;
582+ this . _query2 = new QueryRef ( dirIndex , setter , query , queryList , this ) ;
570583 } else {
571584 throw new QueryError ( ) ;
572585 }
@@ -758,42 +771,54 @@ class ElementInjectorInlineStrategy implements _ElementInjectorStrategy {
758771 if ( p . binding0 instanceof DirectiveBinding ) {
759772 this . _ei . _buildQueriesForDeps (
760773 < DirectiveDependency [ ] > p . binding0 . resolvedFactories [ 0 ] . dependencies ) ;
774+
775+ this . _ei . _buildQueriesForDirective ( 0 , ( < DirectiveBinding > p . binding0 ) . queries ) ;
761776 }
762777 if ( p . binding1 instanceof DirectiveBinding ) {
763778 this . _ei . _buildQueriesForDeps (
764779 < DirectiveDependency [ ] > p . binding1 . resolvedFactories [ 0 ] . dependencies ) ;
780+
781+ this . _ei . _buildQueriesForDirective ( 1 , ( < DirectiveBinding > p . binding1 ) . queries ) ;
765782 }
766783 if ( p . binding2 instanceof DirectiveBinding ) {
767784 this . _ei . _buildQueriesForDeps (
768785 < DirectiveDependency [ ] > p . binding2 . resolvedFactories [ 0 ] . dependencies ) ;
786+ this . _ei . _buildQueriesForDirective ( 2 , ( < DirectiveBinding > p . binding2 ) . queries ) ;
769787 }
770788 if ( p . binding3 instanceof DirectiveBinding ) {
771789 this . _ei . _buildQueriesForDeps (
772790 < DirectiveDependency [ ] > p . binding3 . resolvedFactories [ 0 ] . dependencies ) ;
791+ this . _ei . _buildQueriesForDirective ( 3 , ( < DirectiveBinding > p . binding3 ) . queries ) ;
773792 }
774793 if ( p . binding4 instanceof DirectiveBinding ) {
775794 this . _ei . _buildQueriesForDeps (
776795 < DirectiveDependency [ ] > p . binding4 . resolvedFactories [ 0 ] . dependencies ) ;
796+ this . _ei . _buildQueriesForDirective ( 4 , ( < DirectiveBinding > p . binding4 ) . queries ) ;
777797 }
778798 if ( p . binding5 instanceof DirectiveBinding ) {
779799 this . _ei . _buildQueriesForDeps (
780800 < DirectiveDependency [ ] > p . binding5 . resolvedFactories [ 0 ] . dependencies ) ;
801+ this . _ei . _buildQueriesForDirective ( 5 , ( < DirectiveBinding > p . binding5 ) . queries ) ;
781802 }
782803 if ( p . binding6 instanceof DirectiveBinding ) {
783804 this . _ei . _buildQueriesForDeps (
784805 < DirectiveDependency [ ] > p . binding6 . resolvedFactories [ 0 ] . dependencies ) ;
806+ this . _ei . _buildQueriesForDirective ( 6 , ( < DirectiveBinding > p . binding6 ) . queries ) ;
785807 }
786808 if ( p . binding7 instanceof DirectiveBinding ) {
787809 this . _ei . _buildQueriesForDeps (
788810 < DirectiveDependency [ ] > p . binding7 . resolvedFactories [ 0 ] . dependencies ) ;
811+ this . _ei . _buildQueriesForDirective ( 7 , ( < DirectiveBinding > p . binding7 ) . queries ) ;
789812 }
790813 if ( p . binding8 instanceof DirectiveBinding ) {
791814 this . _ei . _buildQueriesForDeps (
792815 < DirectiveDependency [ ] > p . binding8 . resolvedFactories [ 0 ] . dependencies ) ;
816+ this . _ei . _buildQueriesForDirective ( 8 , ( < DirectiveBinding > p . binding8 ) . queries ) ;
793817 }
794818 if ( p . binding9 instanceof DirectiveBinding ) {
795819 this . _ei . _buildQueriesForDeps (
796820 < DirectiveDependency [ ] > p . binding9 . resolvedFactories [ 0 ] . dependencies ) ;
821+ this . _ei . _buildQueriesForDirective ( 9 , ( < DirectiveBinding > p . binding9 ) . queries ) ;
797822 }
798823 }
799824
@@ -896,6 +921,7 @@ class ElementInjectorDynamicStrategy implements _ElementInjectorStrategy {
896921 if ( p . bindings [ i ] instanceof DirectiveBinding ) {
897922 this . _ei . _buildQueriesForDeps (
898923 < DirectiveDependency [ ] > p . bindings [ i ] . resolvedFactory . dependencies ) ;
924+ this . _ei . _buildQueriesForDirective ( i , ( < DirectiveBinding > p . bindings [ i ] ) . queries ) ;
899925 }
900926 }
901927 }
@@ -927,15 +953,22 @@ export class QueryError extends BaseException {
927953}
928954
929955export class QueryRef {
930- constructor ( public query : QueryMetadata , public list : QueryList < any > ,
931- public originator : ElementInjector , public dirty : boolean = true ) { }
956+ constructor ( public dirIndex : number , public setter : SetterFn , public query : QueryMetadata ,
957+ public list : QueryList < any > , public originator : ElementInjector ,
958+ public dirty : boolean = true ) { }
932959
933960 get isViewQuery ( ) : boolean { return this . query . isViewQuery ; }
934961
935962 update ( ) : void {
936963 if ( ! this . dirty ) return ;
937964 this . _update ( ) ;
938965 this . dirty = false ;
966+
967+ // TODO delete the check once only field queries are supported
968+ if ( isPresent ( this . dirIndex ) ) {
969+ var dir = this . originator . getDirectiveAtIndex ( this . dirIndex ) ;
970+ this . setter ( dir , this . list ) ;
971+ }
939972 }
940973
941974 private _update ( ) : void {
0 commit comments