1
1
import { Injectable } from 'angular2/di' ;
2
2
3
3
import { 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' ;
5
5
import { reflector } from 'angular2/src/reflection/reflection' ;
6
6
7
7
import {
@@ -309,7 +309,7 @@ function _createElementBinders(protoView, elementBinders, allDirectiveBindings)
309
309
componentDirectiveBinding , directiveBindings ) ;
310
310
311
311
_createElementBinder ( protoView , i , renderElementBinder , protoElementInjector ,
312
- componentDirectiveBinding ) ;
312
+ componentDirectiveBinding , directiveBindings ) ;
313
313
}
314
314
}
315
315
@@ -343,28 +343,20 @@ function _createProtoElementInjector(binderIndex, parentPeiWithDistance, renderE
343
343
parentPeiWithDistance . protoElementInjector , binderIndex , directiveBindings ,
344
344
isPresent ( componentDirectiveBinding ) , parentPeiWithDistance . distance ) ;
345
345
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
- }
356
346
}
357
347
return protoElementInjector ;
358
348
}
359
349
360
350
function _createElementBinder ( protoView , boundElementIndex , renderElementBinder ,
361
- protoElementInjector , componentDirectiveBinding ) : ElementBinder {
351
+ protoElementInjector , componentDirectiveBinding , directiveBindings ) : ElementBinder {
362
352
var parent = null ;
363
353
if ( renderElementBinder . parentIndex !== - 1 ) {
364
354
parent = protoView . elementBinders [ renderElementBinder . parentIndex ] ;
365
355
}
356
+
357
+ var directiveVariableBindings = createDirectiveVariableBindings ( renderElementBinder , directiveBindings ) ;
366
358
var elBinder = protoView . bindElement ( parent , renderElementBinder . distanceToParent ,
367
- protoElementInjector , componentDirectiveBinding ) ;
359
+ protoElementInjector , directiveVariableBindings , componentDirectiveBinding ) ;
368
360
protoView . bindEvent ( renderElementBinder . eventBindings , boundElementIndex , - 1 ) ;
369
361
// variables
370
362
// 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,
377
369
return elBinder ;
378
370
}
379
371
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
+
380
415
function _bindDirectiveEvents ( protoView , elementBinders : List < renderApi . ElementBinder > ) {
381
416
for ( var boundElementIndex = 0 ; boundElementIndex < elementBinders . length ; ++ boundElementIndex ) {
382
417
var dirs = elementBinders [ boundElementIndex ] . directives ;
0 commit comments