@@ -28,11 +28,12 @@ import {
2828 assertionsEnabled ,
2929 isJsObject ,
3030 global ,
31- stringify
31+ stringify ,
32+ CONST
3233} from 'angular2/src/facade/lang' ;
3334import { PromiseWrapper , EventEmitter , ObservableWrapper } from 'angular2/src/facade/async' ;
3435
35- import { Injector , bind , Injectable } from 'angular2/di' ;
36+ import { Injector , bind , Injectable , Binding , FORWARD_REF } from 'angular2/di' ;
3637import {
3738 PipeRegistry ,
3839 defaultPipeRegistry ,
@@ -54,6 +55,7 @@ import {
5455 Query
5556} from 'angular2/annotations' ;
5657import * as viewAnn from 'angular2/src/core/annotations_impl/view' ;
58+ import * as visAnn from 'angular2/src/core/annotations_impl/visibility' ;
5759
5860import { QueryList } from 'angular2/src/core/compiler/query_list' ;
5961
@@ -985,6 +987,43 @@ export function main() {
985987 var comp = view . rawView . locals . get ( "dir" ) ;
986988 expect ( comp . directive . injectable ) . toBeAnInstanceOf ( InjectableService ) ;
987989
990+ async . done ( ) ;
991+ } ) ;
992+ } ) ) ;
993+
994+ it ( "should support the event-bus scenario" ,
995+ inject ( [ TestBed , AsyncTestCompleter ] , ( tb , async ) => {
996+ tb . overrideView ( MyComp , new viewAnn . View ( {
997+ template : `
998+ <grand-parent-providing-event-bus>
999+ <parent-providing-event-bus>
1000+ <child-consuming-event-bus>
1001+ </child-consuming-event-bus>
1002+ </parent-providing-event-bus>
1003+ </grand-parent-providing-event-bus>
1004+ ` ,
1005+ directives : [
1006+ GrandParentProvidingEventBus ,
1007+ ParentProvidingEventBus ,
1008+ ChildConsumingEventBus
1009+ ]
1010+ } ) ) ;
1011+ tb . createView ( MyComp , { context : ctx } )
1012+ . then ( ( view ) => {
1013+ var eis = view . rawView . elementInjectors ;
1014+ var childRawView = view . rawView . componentChildViews [ 1 ] ;
1015+
1016+ var grandParent = eis [ 0 ] . get ( GrandParentProvidingEventBus ) ;
1017+ var parent = eis [ 1 ] . get ( ParentProvidingEventBus ) ;
1018+ var child1 = eis [ 2 ] . get ( ChildConsumingEventBus ) ;
1019+ var child2 = childRawView . elementInjectors [ 0 ] . get ( ChildConsumingEventBus ) ;
1020+
1021+ expect ( grandParent . bus . name ) . toEqual ( "grandparent" ) ;
1022+ expect ( parent . bus . name ) . toEqual ( "parent" ) ;
1023+ expect ( parent . grandParentBus ) . toBe ( grandParent . bus ) ;
1024+ expect ( child1 . bus ) . toBe ( parent . bus ) ;
1025+ expect ( child2 . bus ) . toBe ( parent . bus ) ;
1026+
9881027 async . done ( ) ;
9891028 } ) ;
9901029 } ) ) ;
@@ -1540,3 +1579,64 @@ class DirectiveConsumingInjectableUnbounded {
15401579 parent . directive = this ;
15411580 }
15421581}
1582+
1583+
1584+ @CONST ( )
1585+ class EventBus {
1586+ parentEventBus : EventBus ;
1587+ name : string ;
1588+
1589+ constructor ( parentEventBus : EventBus , name : string ) {
1590+ this . parentEventBus = parentEventBus ;
1591+ this . name = name ;
1592+ }
1593+ }
1594+
1595+ @Directive ( {
1596+ selector : 'grand-parent-providing-event-bus' ,
1597+ hostInjector : [ new Binding ( EventBus , { toValue : new EventBus ( null , "grandparent" ) } ) ]
1598+ } )
1599+ class GrandParentProvidingEventBus {
1600+ bus : EventBus ;
1601+
1602+ constructor ( bus : EventBus ) { this . bus = bus ; }
1603+ }
1604+
1605+ function createParentBusHost ( peb ) {
1606+ return new EventBus ( peb , "parent" ) ;
1607+ }
1608+
1609+ function createParentBusView ( p ) {
1610+ return p . bus ;
1611+ }
1612+ @Component ( {
1613+ selector : 'parent-providing-event-bus' ,
1614+ hostInjector : [ new Binding (
1615+ EventBus , { toFactory : createParentBusHost , deps : [ [ EventBus , new visAnn . Unbounded ( ) ] ] } ) ] ,
1616+ viewInjector : [ new Binding (
1617+ EventBus ,
1618+ { toFactory : createParentBusView , deps : [ [ FORWARD_REF ( ( ) => ParentProvidingEventBus ) ] ] } ) ]
1619+ } )
1620+ @View ( {
1621+ directives : [ FORWARD_REF ( ( ) => ChildConsumingEventBus ) ] ,
1622+ template : `
1623+ <child-consuming-event-bus></child-consuming-event-bus>
1624+ `
1625+ } )
1626+ class ParentProvidingEventBus {
1627+ bus : EventBus ;
1628+ grandParentBus : EventBus ;
1629+
1630+ constructor ( bus : EventBus , @Unbounded ( ) grandParentBus : EventBus ) {
1631+ // constructor(bus: EventBus) {
1632+ this . bus = bus ;
1633+ this . grandParentBus = grandParentBus ;
1634+ }
1635+ }
1636+
1637+ @Directive ( { selector : 'child-consuming-event-bus' } )
1638+ class ChildConsumingEventBus {
1639+ bus : EventBus ;
1640+
1641+ constructor ( @Unbounded ( ) bus : EventBus ) { this . bus = bus ; }
1642+ }
0 commit comments