11import { Injectable } from 'angular2/di' ;
22
33import { List , ListWrapper , MapWrapper } from 'angular2/src/facade/collection' ;
4- import { isPresent , isBlank } from 'angular2/src/facade/lang' ;
4+ import { isPresent , isBlank , BaseException } from 'angular2/src/facade/lang' ;
55import { reflector } from 'angular2/src/reflection/reflection' ;
66
77import {
@@ -309,7 +309,7 @@ function _createElementBinders(protoView, elementBinders, allDirectiveBindings)
309309 componentDirectiveBinding , directiveBindings ) ;
310310
311311 _createElementBinder ( protoView , i , renderElementBinder , protoElementInjector ,
312- componentDirectiveBinding ) ;
312+ componentDirectiveBinding , directiveBindings ) ;
313313 }
314314}
315315
@@ -343,28 +343,20 @@ function _createProtoElementInjector(binderIndex, parentPeiWithDistance, renderE
343343 parentPeiWithDistance . protoElementInjector , binderIndex , directiveBindings ,
344344 isPresent ( componentDirectiveBinding ) , parentPeiWithDistance . distance ) ;
345345 protoElementInjector . attributes = renderElementBinder . readAttributes ;
346- if ( hasVariables ) {
347- protoElementInjector . exportComponent = isPresent ( componentDirectiveBinding ) ;
348- protoElementInjector . exportElement = isBlank ( componentDirectiveBinding ) ;
349-
350- // experiment
351- var exportImplicitName = MapWrapper . get ( renderElementBinder . variableBindings , '\$implicit' ) ;
352- if ( isPresent ( exportImplicitName ) ) {
353- protoElementInjector . exportImplicitName = exportImplicitName ;
354- }
355- }
356346 }
357347 return protoElementInjector ;
358348}
359349
360350function _createElementBinder ( protoView , boundElementIndex , renderElementBinder ,
361- protoElementInjector , componentDirectiveBinding ) : ElementBinder {
351+ protoElementInjector , componentDirectiveBinding , directiveBindings ) : ElementBinder {
362352 var parent = null ;
363353 if ( renderElementBinder . parentIndex !== - 1 ) {
364354 parent = protoView . elementBinders [ renderElementBinder . parentIndex ] ;
365355 }
356+
357+ var directiveVariableBindings = createDirectiveVariableBindings ( renderElementBinder , directiveBindings ) ;
366358 var elBinder = protoView . bindElement ( parent , renderElementBinder . distanceToParent ,
367- protoElementInjector , componentDirectiveBinding ) ;
359+ protoElementInjector , directiveVariableBindings , componentDirectiveBinding ) ;
368360 protoView . bindEvent ( renderElementBinder . eventBindings , boundElementIndex , - 1 ) ;
369361 // variables
370362 // The view's locals needs to have a full set of variable names at construction time
@@ -377,6 +369,49 @@ function _createElementBinder(protoView, boundElementIndex, renderElementBinder,
377369 return elBinder ;
378370}
379371
372+ export function createDirectiveVariableBindings ( renderElementBinder :renderApi . ElementBinder ,
373+ directiveBindings :List < DirectiveBinding > ) : Map < String , number > {
374+ var directiveVariableBindings = MapWrapper . create ( ) ;
375+ MapWrapper . forEach ( renderElementBinder . variableBindings , ( templateName , exportAs ) => {
376+ var dirIndex = _findDirectiveIndexByExportAs ( renderElementBinder , directiveBindings , exportAs ) ;
377+ MapWrapper . set ( directiveVariableBindings , templateName , dirIndex ) ;
378+ } ) ;
379+ return directiveVariableBindings ;
380+ }
381+
382+ function _findDirectiveIndexByExportAs ( renderElementBinder , directiveBindings , exportAs ) {
383+ var matchedDirectiveIndex = null ;
384+ var matchedDirective ;
385+
386+ for ( var i = 0 ; i < directiveBindings . length ; ++ i ) {
387+ var directive = directiveBindings [ i ] ;
388+
389+ if ( _directiveExportAs ( directive ) == exportAs ) {
390+ if ( isPresent ( matchedDirective ) ) {
391+ throw new BaseException ( `More than one directive have exportAs = '${ exportAs } '. Directives: [${ matchedDirective . displayName } , ${ directive . displayName } ]` ) ;
392+ }
393+
394+ matchedDirectiveIndex = i ;
395+ matchedDirective = directive ;
396+ }
397+ }
398+
399+ if ( isBlank ( matchedDirective ) && exportAs !== "$implicit" ) {
400+ throw new BaseException ( `Cannot find directive with exportAs = '${ exportAs } '` ) ;
401+ }
402+
403+ return matchedDirectiveIndex ;
404+ }
405+
406+ function _directiveExportAs ( directive ) :string {
407+ var directiveExportAs = directive . metadata . exportAs ;
408+ if ( isBlank ( directiveExportAs ) && directive . metadata . type === renderApi . DirectiveMetadata . COMPONENT_TYPE ) {
409+ return "$implicit" ;
410+ } else {
411+ return directiveExportAs ;
412+ }
413+ }
414+
380415function _bindDirectiveEvents ( protoView , elementBinders : List < renderApi . ElementBinder > ) {
381416 for ( var boundElementIndex = 0 ; boundElementIndex < elementBinders . length ; ++ boundElementIndex ) {
382417 var dirs = elementBinders [ boundElementIndex ] . directives ;
0 commit comments