cleanup(view): changed ComponentRef to contain ElementRef instead of extending it

This commit is contained in:
vsavkin 2015-04-13 09:35:56 -07:00
parent b5c9f9ed9b
commit 8b97cf1479
6 changed files with 39 additions and 39 deletions

View File

@ -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';

View File

@ -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 {

View File

@ -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]);
});
}

View File

@ -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);
}

View File

@ -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'})

View File

@ -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);
}
}