From 8b97cf14797a8f01707be316d9996bd0bdae8998 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Mon, 13 Apr 2015 09:35:56 -0700 Subject: [PATCH] cleanup(view): changed ComponentRef to contain ElementRef instead of extending it --- modules/angular2/core.js | 2 +- modules/angular2/src/core/application.js | 3 +- .../core/compiler/dynamic_component_loader.js | 34 ++++++++++++++++--- .../src/core/compiler/element_injector.js | 31 +++-------------- .../compiler/dynamic_component_loader_spec.js | 2 +- .../test/core/compiler/integration_spec.js | 6 ++-- 6 files changed, 39 insertions(+), 39 deletions(-) diff --git a/modules/angular2/core.js b/modules/angular2/core.js index 6c8cad046b..8f1cb18d7f 100644 --- a/modules/angular2/core.js +++ b/modules/angular2/core.js @@ -16,7 +16,7 @@ export * from './src/core/compiler/compiler'; // TODO(tbosch): remove this once render migration is complete export * from 'angular2/src/render/dom/compiler/template_loader'; export * from './src/core/compiler/dynamic_component_loader'; -export {ElementRef, DirectiveRef, ComponetRef} from './src/core/compiler/element_injector'; +export {ElementRef, ComponetRef} from './src/core/compiler/element_injector'; export * from './src/core/compiler/view'; export * from './src/core/compiler/view_container'; diff --git a/modules/angular2/src/core/application.js b/modules/angular2/src/core/application.js index 2856aab2e1..1f240e8ac3 100644 --- a/modules/angular2/src/core/application.js +++ b/modules/angular2/src/core/application.js @@ -25,14 +25,13 @@ import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mappe import {UrlResolver} from 'angular2/src/services/url_resolver'; import {StyleUrlResolver} from 'angular2/src/render/dom/shadow_dom/style_url_resolver'; import {StyleInliner} from 'angular2/src/render/dom/shadow_dom/style_inliner'; -import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader'; +import {ComponentRef, DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader'; import {TestabilityRegistry, Testability} from 'angular2/src/core/testability/testability'; import {ViewFactory, VIEW_POOL_CAPACITY} from 'angular2/src/core/compiler/view_factory'; import {ProtoViewFactory} from 'angular2/src/core/compiler/proto_view_factory'; import {Renderer} from 'angular2/src/render/api'; import {DirectDomRenderer} from 'angular2/src/render/dom/direct_dom_renderer'; import * as rc from 'angular2/src/render/dom/compiler/compiler'; -import {ComponentRef} from 'angular2/src/core/compiler/element_injector'; import * as rvf from 'angular2/src/render/dom/view/view_factory'; import { diff --git a/modules/angular2/src/core/compiler/dynamic_component_loader.js b/modules/angular2/src/core/compiler/dynamic_component_loader.js index c71154f6e0..8e00298ab9 100644 --- a/modules/angular2/src/core/compiler/dynamic_component_loader.js +++ b/modules/angular2/src/core/compiler/dynamic_component_loader.js @@ -7,7 +7,29 @@ import {Promise} from 'angular2/src/facade/async'; import {Component} from 'angular2/src/core/annotations/annotations'; import {ViewFactory} from 'angular2/src/core/compiler/view_factory'; import {Renderer} from 'angular2/src/render/api'; -import {ElementRef, DirectiveRef, ComponentRef} from './element_injector'; +import {ElementRef} from './element_injector'; +import {AppView} from './view'; + + +export class ComponentRef { + location:ElementRef; + instance:any; + componentView:AppView; + + constructor(location:ElementRef, instance:any, componentView:AppView){ + this.location = location; + this.instance = instance; + this.componentView = componentView; + } + + get injector() { + return this.location.injector; + } + + get hostView() { + return this.location.hostView; + } +} /** * Service for dynamically loading a Component into an arbitrary position in the internal Angular @@ -43,8 +65,8 @@ export class DynamicComponentLoader { var hostView = location.hostView; return this._compiler.compile(type).then(componentProtoView => { - var context = hostEi.dynamicallyCreateComponent(type, directiveMetadata.annotation, inj); - var componentView = this._instantiateAndHydrateView(componentProtoView, injector, hostEi, context); + var component = hostEi.dynamicallyCreateComponent(type, directiveMetadata.annotation, inj); + var componentView = this._instantiateAndHydrateView(componentProtoView, injector, hostEi, component); //TODO(vsavkin): do not use component child views as we need to clear the dynamically created views //same problem exists on the render side @@ -54,7 +76,7 @@ export class DynamicComponentLoader { // TODO(vsavkin): return a component ref that dehydrates the component view and removes it // from the component child views - return new ComponentRef(Key.get(type), hostEi, componentView); + return new ComponentRef(location, component, componentView); }); } @@ -75,7 +97,9 @@ export class DynamicComponentLoader { var hostView = this._instantiateAndHydrateView(pv, inj, null, new Object()); // TODO(vsavkin): return a component ref that dehydrates the host view - return new ComponentRef(Key.get(type), hostView.elementInjectors[0], hostView.componentChildViews[0]); + var newLocation = new ElementRef(hostView.elementInjectors[0]); + var component = hostView.elementInjectors[0].getComponent(); + return new ComponentRef(newLocation, component, hostView.componentChildViews[0]); }); } diff --git a/modules/angular2/src/core/compiler/element_injector.js b/modules/angular2/src/core/compiler/element_injector.js index fcd063351c..6ba3a15d79 100644 --- a/modules/angular2/src/core/compiler/element_injector.js +++ b/modules/angular2/src/core/compiler/element_injector.js @@ -39,34 +39,12 @@ export class ElementRef { } } -export class DirectiveRef extends ElementRef { - _key:Key; - - constructor(key:Key, elementInjector:ElementInjector){ - super(elementInjector); - this._key = key; - } - - get instance() { - return this.elementInjector.get(this._key); - } -} - -export class ComponentRef extends DirectiveRef { - componentView:viewModule.AppView; - - constructor(key:Key, elementInjector:ElementInjector, componentView:viewModule.AppView){ - super(key, elementInjector); - this.componentView = componentView; - } -} - class StaticKeys { viewId:number; ngElementId:number; viewContainerId:number; bindingPropagationConfigId:number; - directiveRefId:number; + elementRefId:number; constructor() { //TODO: vsavkin Key.annotate(Key.get(AppView), 'static') @@ -74,7 +52,7 @@ class StaticKeys { this.ngElementId = Key.get(NgElement).id; this.viewContainerId = Key.get(ViewContainer).id; this.bindingPropagationConfigId = Key.get(BindingPropagationConfig).id; - this.directiveRefId = Key.get(DirectiveRef).id; + this.elementRefId = Key.get(ElementRef).id; } static instance() { @@ -699,9 +677,8 @@ export class ElementInjector extends TreeNode { if (isPresent(dep.propSetterName)) return this._buildPropSetter(dep); if (isPresent(dep.attributeName)) return this._buildAttribute(dep); if (isPresent(dep.queryDirective)) return this._findQuery(dep.queryDirective).list; - if (dep.key.id === StaticKeys.instance().directiveRefId) { - // TODO: we need store component view here and pass it to directive ref - return new DirectiveRef(requestor, this); + if (dep.key.id === StaticKeys.instance().elementRefId) { + return new ElementRef(this); } return this._getByKey(dep.key, dep.depth, dep.optional, requestor); } diff --git a/modules/angular2/test/core/compiler/dynamic_component_loader_spec.js b/modules/angular2/test/core/compiler/dynamic_component_loader_spec.js index 0f4e6461c6..95ca21ce1d 100644 --- a/modules/angular2/test/core/compiler/dynamic_component_loader_spec.js +++ b/modules/angular2/test/core/compiler/dynamic_component_loader_spec.js @@ -1,6 +1,6 @@ import {ddescribe, describe, it, iit, expect, beforeEach} from 'angular2/test_lib'; import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader'; -import {DynamicComponentLoader, DirectiveRef} from 'angular2/src/core/compiler/dynamic_component_loader'; +import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader'; import {Decorator, Viewport} from 'angular2/src/core/annotations/annotations'; @Decorator({selector: 'someDecorator'}) diff --git a/modules/angular2/test/core/compiler/integration_spec.js b/modules/angular2/test/core/compiler/integration_spec.js index e0d57324cf..493ac409d4 100644 --- a/modules/angular2/test/core/compiler/integration_spec.js +++ b/modules/angular2/test/core/compiler/integration_spec.js @@ -29,7 +29,7 @@ import {View} from 'angular2/src/core/annotations/view'; import {Parent, Ancestor} from 'angular2/src/core/annotations/visibility'; import {EventEmitter, Attribute} from 'angular2/src/core/annotations/di'; import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader'; -import {DirectiveRef} from 'angular2/src/core/compiler/element_injector'; +import {ElementRef} from 'angular2/src/core/compiler/element_injector'; import {If} from 'angular2/src/directives/if'; @@ -726,8 +726,8 @@ class DynamicallyCreatedComponentService { }) class DynamicComp { done; - constructor(loader:DynamicComponentLoader, self:DirectiveRef) { - this.done = loader.loadIntoExistingLocation(DynamicallyCreatedCmp, self); + constructor(loader:DynamicComponentLoader, location:ElementRef) { + this.done = loader.loadIntoExistingLocation(DynamicallyCreatedCmp, location); } }