refactor(core): remove DynamicComponent

BREAKING CHANGE:
A dynamic component is just a component that has no @View annotation…
This commit is contained in:
Tobias Bosch
2015-04-29 16:52:34 -07:00
parent b71fe311fc
commit 8faf6364dc
17 changed files with 76 additions and 123 deletions

View File

@ -7,7 +7,5 @@ export {
Component as ComponentAnnotation,
Decorator as DecoratorAnnotation,
Directive as DirectiveAnnotation,
DynamicComponent as DynamicComponentAnnotation,
Viewport as ViewportAnnotation,
onDestroy, onChange, onAllChangesDone
} from '../annotations_impl/annotations';

View File

@ -8,7 +8,7 @@ import {DEFAULT} from 'angular2/change_detection';
/**
* Directives allow you to attach behavior to elements in the DOM.
*
* Directive is an abstract concept, instead use concrete directives: {@link Component}, {@link DynamicComponent}, {@link Decorator}.
* Directive is an abstract concept, instead use concrete directives: {@link Component}, or {@link Decorator}.
*
* A directive consists of a single directive annotation and a controller class. When the directive's `selector` matches
* elements in the DOM, the following steps occur:
@ -542,6 +542,51 @@ export class Directive extends Injectable {
* }
* ```
*
*
* Dynamically loading a component at runtime:
*
* Regular Angular components are statically resolved. Dynamic components allows to resolve a component at runtime
* instead by providing a placeholder into which a regular Angular component can be dynamically loaded. Once loaded,
* the dynamically-loaded component becomes permanent and cannot be changed.
* Dynamic components are declared just like components, but without a `@View` annotation.
*
*
* ## Example
*
* Here we have `DynamicComp` which acts as the placeholder for `HelloCmp`. At runtime, the dynamic component
* `DynamicComp` requests loading of the `HelloCmp` component.
*
* There is nothing special about `HelloCmp`, which is a regular Angular component. It can also be used in other static
* locations.
*
* ```
* @Component({
* selector: 'dynamic-comp'
* })
* class DynamicComp {
* helloCmp:HelloCmp;
* constructor(loader:DynamicComponentLoader, location:ElementRef) {
* loader.load(HelloCmp, location).then((helloCmp) => {
* this.helloCmp = helloCmp;
* });
* }
* }
*
* @Component({
* selector: 'hello-cmp'
* })
* @View({
* template: "{{greeting}}"
* })
* class HelloCmp {
* greeting:string;
* constructor() {
* this.greeting = "hello";
* }
* }
* ```
*
*
* @exportedAs angular2/annotations
*/
export class Component extends Directive {
@ -639,90 +684,6 @@ export class Component extends Directive {
}
}
/**
* Directive used for dynamically loading components.
*
* Regular Angular components are statically resolved. DynamicComponent allows to you resolve a component at runtime
* instead by providing a placeholder into which a regular Angular component can be dynamically loaded. Once loaded,
* the dynamically-loaded component becomes permanent and cannot be changed.
*
*
* ## Example
*
* Here we have `DynamicComp` which acts as the placeholder for `HelloCmp`. At runtime, the dynamic component
* `DynamicComp` requests loading of the `HelloCmp` component.
*
* There is nothing special about `HelloCmp`, which is a regular Angular component. It can also be used in other static
* locations.
*
* ```
* @DynamicComponent({
* selector: 'dynamic-comp'
* })
* class DynamicComp {
* helloCmp:HelloCmp;
* constructor(loader:DynamicComponentLoader, location:PrivateComponentLocation) {
* loader.load(HelloCmp, location).then((helloCmp) => {
* this.helloCmp = helloCmp;
* });
* }
* }
*
* @Component({
* selector: 'hello-cmp'
* })
* @View({
* template: "{{greeting}}"
* })
* class HelloCmp {
* greeting:string;
* constructor() {
* this.greeting = "hello";
* }
* }
* ```
*
*
*
* @exportedAs angular2/annotations
*/
export class DynamicComponent extends Directive {
/**
* Same as `injectables` in the {@link Component}.
*/
// TODO(vsankin): Please extract into AbstractComponent
injectables:any; //List;
@CONST()
constructor({
selector,
properties,
events,
hostListeners,
hostProperties,
injectables,
lifecycle
}:{
selector:string,
properties:any,
events:List,
hostListeners:any,
hostProperties:any,
injectables:List,
lifecycle:List
}={}) {
super({
selector: selector,
properties: properties,
events: events,
hostListeners: hostListeners,
hostProperties: hostProperties,
lifecycle: lifecycle
});
this.injectables = injectables;
}
}
/**
* Directive that attaches behavior to DOM elements.

View File

@ -4,7 +4,7 @@ import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection';
import {DirectiveMetadataReader} from './directive_metadata_reader';
import {Component, DynamicComponent, Decorator} from '../annotations_impl/annotations';
import {Component, Decorator} from '../annotations_impl/annotations';
import {AppProtoView} from './view';
import {ProtoViewRef} from './view_ref';
import {DirectiveBinding} from './element_injector';
@ -128,8 +128,10 @@ export class Compiler {
// It happens when a template references a component multiple times.
return pvPromise;
}
var template = this._templateResolver.resolve(component);
if (isBlank(template)) {
return null;
}
if (isPresent(template.renderer)) {
var directives = [];
pvPromise = this._renderer.createImperativeComponentProtoView(template.renderer).then( (renderPv) => {
@ -174,9 +176,7 @@ export class Compiler {
};
var nestedCall = null;
if (isPresent(nestedComponent)) {
if (!(nestedComponent.annotation instanceof DynamicComponent)) {
nestedCall = this._compile(nestedComponent);
}
nestedCall = this._compile(nestedComponent);
} else if (isPresent(nestedRenderProtoView)) {
nestedCall = this._compileNestedProtoViews(componentBinding, nestedRenderProtoView, directives, false);
}
@ -231,7 +231,7 @@ export class Compiler {
var ann = directiveBinding.annotation;
var renderType;
var compileChildren = true;
if ((ann instanceof Component) || (ann instanceof DynamicComponent)) {
if (ann instanceof Component) {
renderType = renderApi.DirectiveMetadata.COMPONENT_TYPE;
} else if (ann instanceof Decorator) {
renderType = renderApi.DirectiveMetadata.DECORATOR_TYPE;

View File

@ -4,7 +4,7 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {reflector} from 'angular2/src/reflection/reflection';
import {ChangeDetection, DirectiveIndex} from 'angular2/change_detection';
import {Component, DynamicComponent} from '../annotations_impl/annotations';
import {Component} from '../annotations_impl/annotations';
import * as renderApi from 'angular2/src/render/api';
import {AppProtoView} from './view';
@ -162,7 +162,7 @@ class SortedDirectives {
this.componentDirective = null;
ListWrapper.forEach(renderDirectives, (renderDirectiveBinder) => {
var directiveBinding = allDirectives[renderDirectiveBinder.directiveIndex];
if ((directiveBinding.annotation instanceof Component) || (directiveBinding.annotation instanceof DynamicComponent)) {
if (directiveBinding.annotation instanceof Component) {
// component directives need to be the first binding in ElementInjectors!
this.componentDirective = directiveBinding;
ListWrapper.insert(this.renderDirectives, 0, renderDirectiveBinder);

View File

@ -34,7 +34,7 @@ export class TemplateResolver {
return annotation;
}
}
throw new BaseException(`No template found for ${stringify(component)}`);
// No annotation = dynamic component!
return null;
}
}

View File

@ -1,8 +1,7 @@
import {
ComponentAnnotation,
DecoratorAnnotation,
DynamicComponentAnnotation,
ViewportAnnotation} from '../annotations/annotations';
DecoratorAnnotation
} from '../annotations/annotations';
import {ViewAnnotation} from '../annotations/view';
import {AncestorAnnotation, ParentAnnotation} from '../annotations/visibility';
import {AttributeAnnotation, QueryAnnotation} from '../annotations/di';
@ -25,8 +24,6 @@ function makeDecorator(annotationCls) {
/* from annotations */
export var Component = makeDecorator(ComponentAnnotation);
export var Decorator = makeDecorator(DecoratorAnnotation);
export var DynamicComponent = makeDecorator(DynamicComponentAnnotation);
export var Viewport = makeDecorator(ViewportAnnotation);
/* from di */
export var Attribute = makeDecorator(AttributeAnnotation);