@@ -2,12 +2,11 @@ import {Key, Injector, Injectable, ResolvedBinding} from 'angular2/di'
22import { Compiler } from './compiler' ;
33import { DirectiveMetadataReader } from './directive_metadata_reader' ;
44import { Type , BaseException , stringify , isPresent } from 'angular2/src/facade/lang' ;
5- import { List } from 'angular2/src/facade/collection' ;
65import { Promise } from 'angular2/src/facade/async' ;
76import { Component } from 'angular2/src/core/annotations/annotations' ;
87import { ViewFactory } from 'angular2/src/core/compiler/view_factory' ;
9- import { Renderer } from 'angular2/src/render/api ' ;
10- import { ElementRef } from './element_injector' ;
8+ import { AppViewHydrator } from 'angular2/src/core/compiler/view_hydrator ' ;
9+ import { ElementRef , DirectiveBinding } from './element_injector' ;
1110import { AppView } from './view' ;
1211
1312/**
@@ -43,13 +42,15 @@ export class ComponentRef {
4342export class DynamicComponentLoader {
4443 _compiler :Compiler ;
4544 _viewFactory :ViewFactory ;
45+ _viewHydrator :AppViewHydrator ;
4646 _directiveMetadataReader :DirectiveMetadataReader ;
4747
4848 constructor ( compiler :Compiler , directiveMetadataReader :DirectiveMetadataReader ,
49- renderer : Renderer , viewFactory : ViewFactory ) {
49+ viewFactory : ViewFactory , viewHydrator : AppViewHydrator ) {
5050 this . _compiler = compiler ;
5151 this . _directiveMetadataReader = directiveMetadataReader ;
52- this . _viewFactory = viewFactory
52+ this . _viewFactory = viewFactory ;
53+ this . _viewHydrator = viewHydrator ;
5354 }
5455
5556 /**
@@ -58,61 +59,44 @@ export class DynamicComponentLoader {
5859 */
5960 loadIntoExistingLocation ( type :Type , location :ElementRef , injector :Injector = null ) :Promise < ComponentRef > {
6061 this . _assertTypeIsComponent ( type ) ;
62+ var annotation = this . _directiveMetadataReader . read ( type ) . annotation ;
63+ var componentBinding = DirectiveBinding . createFromType ( type , annotation ) ;
6164
62- var directiveMetadata = this . _directiveMetadataReader . read ( type ) ;
63-
64- var inj = this . _componentAppInjector ( location , injector , directiveMetadata . resolvedInjectables ) ;
65-
66- var hostEi = location . elementInjector ;
67- var hostView = location . hostView ;
6865 return this . _compiler . compile ( type ) . then ( componentProtoView => {
69- var component = hostEi . dynamicallyCreateComponent ( type , directiveMetadata . annotation , inj ) ;
70- var componentView = this . _instantiateAndHydrateView ( componentProtoView , injector , hostEi , component ) ;
71-
72- //TODO(vsavkin): do not use component child views as we need to clear the dynamically created views
73- //same problem exists on the render side
74- hostView . setDynamicComponentChildView ( location . boundElementIndex , componentView ) ;
66+ var componentView = this . _viewFactory . getView ( componentProtoView ) ;
67+ var hostView = location . hostView ;
68+ this . _viewHydrator . hydrateDynamicComponentView (
69+ hostView , location . boundElementIndex , componentView , componentBinding , injector ) ;
7570
7671 // TODO(vsavkin): return a component ref that dehydrates the component view and removes it
7772 // from the component child views
78- return new ComponentRef ( location , component , componentView ) ;
73+ // See ViewFactory.returnView
74+ // See AppViewHydrator.dehydrateDynamicComponentView
75+ return new ComponentRef ( location , location . elementInjector . getDynamicallyLoadedComponent ( ) , componentView ) ;
7976 } ) ;
8077 }
8178
8279 /**
8380 * Loads a component as a child of the View given by the provided ElementRef. The loaded
8481 * component receives injection normally as a hosted view.
85- *
86- * TODO(vsavkin, jelbourn): remove protoViewFactory after render layer exists.
8782 */
8883 loadIntoNewLocation ( elementOrSelector :any , type :Type , location :ElementRef ,
8984 injector :Injector = null ) :Promise < ComponentRef > {
9085 this . _assertTypeIsComponent ( type ) ;
9186
92- var inj = this . _componentAppInjector ( location , injector , null ) ;
93-
94- //TODO(tbosch) this should always be a selector
95- return this . _compiler . compileRoot ( elementOrSelector , type ) . then ( pv => {
96- var hostView = this . _instantiateAndHydrateView ( pv , inj , null , new Object ( ) ) ;
87+ return this . _compiler . compileInHost ( type ) . then ( hostProtoView => {
88+ var hostView = this . _viewFactory . getView ( hostProtoView ) ;
89+ this . _viewHydrator . hydrateInPlaceHostView ( null , elementOrSelector , hostView , injector ) ;
9790
9891 // TODO(vsavkin): return a component ref that dehydrates the host view
92+ // See ViewFactory.returnView
93+ // See AppViewHydrator.dehydrateInPlaceHostView
9994 var newLocation = new ElementRef ( hostView . elementInjectors [ 0 ] ) ;
10095 var component = hostView . elementInjectors [ 0 ] . getComponent ( ) ;
10196 return new ComponentRef ( newLocation , component , hostView . componentChildViews [ 0 ] ) ;
10297 } ) ;
10398 }
10499
105- _componentAppInjector ( location , injector :Injector , resolvedBindings :List < ResolvedBinding > ) {
106- var inj = isPresent ( injector ) ? injector : location . injector ;
107- return isPresent ( resolvedBindings ) ? inj . createChildFromResolved ( resolvedBindings ) : inj ;
108- }
109-
110- _instantiateAndHydrateView ( protoView , injector , hostElementInjector , context ) {
111- var componentView = this . _viewFactory . getView ( protoView ) ;
112- componentView . hydrate ( injector , hostElementInjector , context , null ) ;
113- return componentView ;
114- }
115-
116100 /** Asserts that the type being dynamically instantiated is a Component. */
117101 _assertTypeIsComponent ( type :Type ) {
118102 var annotation = this . _directiveMetadataReader . read ( type ) . annotation ;
0 commit comments