@@ -16,11 +16,13 @@ export class ComponentRef {
1616 location :ElementRef ;
1717 instance:any ;
1818 componentView:AppView ;
19+ _dispose:Function ;
1920
20- constructor ( location :ElementRef , instance :any , componentView :AppView ) {
21+ constructor ( location :ElementRef , instance :any , componentView :AppView , dispose : Function ) {
2122 this . location = location ;
2223 this . instance = instance ;
2324 this . componentView = componentView ;
25+ this . _dispose = dispose ;
2426 }
2527
2628 get injector ( ) {
@@ -30,6 +32,10 @@ export class ComponentRef {
3032 get hostView ( ) {
3133 return this . location . hostView ;
3234 }
35+
36+ dispose ( ) {
37+ this . _dispose ( ) ;
38+ }
3339}
3440
3541/**
@@ -72,16 +78,16 @@ export class DynamicComponentLoader {
7278 // from the component child views
7379 // See ViewFactory.returnView
7480 // See AppViewHydrator.dehydrateDynamicComponentView
75- return new ComponentRef ( location , location . elementInjector . getDynamicallyLoadedComponent ( ) , componentView ) ;
81+ var dispose = ( ) => { throw "Not implemented" ; } ;
82+ return new ComponentRef ( location , location . elementInjector . getDynamicallyLoadedComponent ( ) , componentView , dispose ) ;
7683 } ) ;
7784 }
7885
7986 /**
80- * Loads a component as a child of the View given by the provided ElementRef . The loaded
81- * component receives injection normally as a hosted view.
87+ * Loads a component in the element specified by elementOrSelector . The loaded component receives
88+ * injection normally as a hosted view.
8289 */
83- loadIntoNewLocation ( elementOrSelector :any , type :Type , location :ElementRef ,
84- injector :Injector = null ) :Promise < ComponentRef > {
90+ loadIntoNewLocation ( elementOrSelector :any , type :Type , injector :Injector = null ) :Promise < ComponentRef > {
8591 this . _assertTypeIsComponent ( type ) ;
8692
8793 return this . _compiler . compileInHost ( type ) . then ( hostProtoView => {
@@ -91,9 +97,30 @@ export class DynamicComponentLoader {
9197 // TODO(vsavkin): return a component ref that dehydrates the host view
9298 // See ViewFactory.returnView
9399 // See AppViewHydrator.dehydrateInPlaceHostView
94- var newLocation = new ElementRef ( hostView . elementInjectors [ 0 ] ) ;
100+ var newLocation = hostView . elementInjectors [ 0 ] . getElementRef ( ) ;
101+ var component = hostView . elementInjectors [ 0 ] . getComponent ( ) ;
102+ var dispose = ( ) => { throw "Not implemented" ; } ;
103+ return new ComponentRef ( newLocation , component , hostView . componentChildViews [ 0 ] , dispose ) ;
104+ } ) ;
105+ }
106+
107+ /**
108+ * Loads a component next to the provided ElementRef. The loaded component receives
109+ * injection normally as a hosted view.
110+ */
111+ loadNextToExistingLocation ( type :Type , location :ElementRef , injector :Injector = null ) :Promise < ComponentRef > {
112+ this . _assertTypeIsComponent ( type ) ;
113+
114+ return this . _compiler . compileInHost ( type ) . then ( hostProtoView => {
115+ var hostView = location . viewContainer . create ( - 1 , hostProtoView , injector ) ;
116+
117+ var newLocation = hostView . elementInjectors [ 0 ] . getElementRef ( ) ;
95118 var component = hostView . elementInjectors [ 0 ] . getComponent ( ) ;
96- return new ComponentRef ( newLocation , component , hostView . componentChildViews [ 0 ] ) ;
119+ var dispose = ( ) => {
120+ var index = location . viewContainer . indexOf ( hostView ) ;
121+ location . viewContainer . remove ( index ) ;
122+ } ;
123+ return new ComponentRef ( newLocation , component , hostView . componentChildViews [ 0 ] , dispose ) ;
97124 } ) ;
98125 }
99126
0 commit comments