feat(core): add syntax sugar to make @View optional
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
import {Injectable} from 'angular2/src/core/di';
|
||||
import {ViewMetadata} from '../metadata/view';
|
||||
import {ComponentMetadata} from '../metadata/directives';
|
||||
|
||||
import {Type, stringify, isBlank} from 'angular2/src/core/facade/lang';
|
||||
import {Type, stringify, isBlank, isPresent} from 'angular2/src/core/facade/lang';
|
||||
import {BaseException} from 'angular2/src/core/facade/exceptions';
|
||||
import {Map, MapWrapper, ListWrapper} from 'angular2/src/core/facade/collection';
|
||||
|
||||
@ -24,13 +25,70 @@ export class ViewResolver {
|
||||
}
|
||||
|
||||
_resolve(component: Type): ViewMetadata {
|
||||
var annotations = reflector.annotations(component);
|
||||
for (var i = 0; i < annotations.length; i++) {
|
||||
var annotation = annotations[i];
|
||||
if (annotation instanceof ViewMetadata) {
|
||||
return annotation;
|
||||
var compMeta: ComponentMetadata;
|
||||
var viewMeta: ViewMetadata;
|
||||
|
||||
reflector.annotations(component).forEach(m => {
|
||||
if (m instanceof ViewMetadata) {
|
||||
viewMeta = m;
|
||||
}
|
||||
if (m instanceof ComponentMetadata) {
|
||||
compMeta = m;
|
||||
}
|
||||
});
|
||||
|
||||
if (isPresent(compMeta)) {
|
||||
if (isBlank(compMeta.template) && isBlank(compMeta.templateUrl) && isBlank(viewMeta)) {
|
||||
throw new BaseException(
|
||||
`Component '${stringify(component)}' must have either 'template', 'templateUrl', or '@View' set.`);
|
||||
|
||||
} else if (isPresent(compMeta.template) && isPresent(viewMeta)) {
|
||||
this._throwMixingViewAndComponent("template", component);
|
||||
|
||||
} else if (isPresent(compMeta.templateUrl) && isPresent(viewMeta)) {
|
||||
this._throwMixingViewAndComponent("templateUrl", component);
|
||||
|
||||
} else if (isPresent(compMeta.directives) && isPresent(viewMeta)) {
|
||||
this._throwMixingViewAndComponent("directives", component);
|
||||
|
||||
} else if (isPresent(compMeta.pipes) && isPresent(viewMeta)) {
|
||||
this._throwMixingViewAndComponent("pipes", component);
|
||||
|
||||
} else if (isPresent(compMeta.encapsulation) && isPresent(viewMeta)) {
|
||||
this._throwMixingViewAndComponent("encapsulation", component);
|
||||
|
||||
} else if (isPresent(compMeta.styles) && isPresent(viewMeta)) {
|
||||
this._throwMixingViewAndComponent("styles", component);
|
||||
|
||||
} else if (isPresent(compMeta.styleUrls) && isPresent(viewMeta)) {
|
||||
this._throwMixingViewAndComponent("styleUrls", component);
|
||||
|
||||
} else if (isPresent(viewMeta)) {
|
||||
return viewMeta;
|
||||
|
||||
} else {
|
||||
return new ViewMetadata({
|
||||
templateUrl: compMeta.templateUrl,
|
||||
template: compMeta.template,
|
||||
directives: compMeta.directives,
|
||||
pipes: compMeta.pipes,
|
||||
encapsulation: compMeta.encapsulation,
|
||||
styles: compMeta.styles,
|
||||
styleUrls: compMeta.styleUrls
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (isBlank(viewMeta)) {
|
||||
throw new BaseException(`No View decorator found on component '${stringify(component)}'`);
|
||||
} else {
|
||||
return viewMeta;
|
||||
}
|
||||
}
|
||||
throw new BaseException(`No View annotation found on component ${stringify(component)}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
_throwMixingViewAndComponent(propertyName: string, component: Type): void {
|
||||
throw new BaseException(
|
||||
`Component '${stringify(component)}' cannot have both '${propertyName}' and '@View' set at the same time"`);
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,11 @@ class Component extends ComponentMetadata {
|
||||
Map<String, String> host,
|
||||
List bindings, String exportAs, String moduleId,
|
||||
Map<String, dynamic> queries,
|
||||
List viewBindings, ChangeDetectionStrategy changeDetection})
|
||||
List viewBindings, ChangeDetectionStrategy changeDetection,
|
||||
String templateUrl, String template, dynamic directives,
|
||||
dynamic pipes, ViewEncapsulation encapsulation, List<String> styles,
|
||||
List<String> styleUrls
|
||||
})
|
||||
: super(
|
||||
selector: selector,
|
||||
inputs: inputs,
|
||||
@ -58,7 +62,15 @@ class Component extends ComponentMetadata {
|
||||
moduleId: moduleId,
|
||||
viewBindings: viewBindings,
|
||||
queries: queries,
|
||||
changeDetection: changeDetection);
|
||||
changeDetection: changeDetection,
|
||||
templateUrl: templateUrl,
|
||||
template: template,
|
||||
directives: directives,
|
||||
pipes: pipes,
|
||||
encapsulation: encapsulation,
|
||||
styles: styles,
|
||||
styleUrls: styleUrls
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -228,6 +228,13 @@ export interface ComponentFactory {
|
||||
queries?: {[key: string]: any},
|
||||
viewBindings?: any[],
|
||||
changeDetection?: ChangeDetectionStrategy,
|
||||
templateUrl?: string,
|
||||
template?: string,
|
||||
styleUrls?: string[],
|
||||
styles?: string[],
|
||||
directives?: Array<Type | any[]>,
|
||||
pipes?: Array<Type | any[]>,
|
||||
encapsulation?: ViewEncapsulation
|
||||
}): ComponentDecorator;
|
||||
new (obj: {
|
||||
selector?: string,
|
||||
@ -242,6 +249,13 @@ export interface ComponentFactory {
|
||||
queries?: {[key: string]: any},
|
||||
viewBindings?: any[],
|
||||
changeDetection?: ChangeDetectionStrategy,
|
||||
templateUrl?: string,
|
||||
template?: string,
|
||||
styleUrls?: string[],
|
||||
styles?: string[],
|
||||
directives?: Array<Type | any[]>,
|
||||
pipes?: Array<Type | any[]>,
|
||||
encapsulation?: ViewEncapsulation
|
||||
}): ComponentMetadata;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import {isPresent, CONST, CONST_EXPR, Type} from 'angular2/src/core/facade/lang';
|
||||
import {InjectableMetadata} from 'angular2/src/core/di/metadata';
|
||||
import {ChangeDetectionStrategy} from 'angular2/src/core/change_detection';
|
||||
import {ViewEncapsulation} from 'angular2/src/core/metadata/view';
|
||||
|
||||
/**
|
||||
* Directives allow you to attach behavior to elements in the DOM.
|
||||
@ -876,8 +877,23 @@ export class ComponentMetadata extends DirectiveMetadata {
|
||||
*/
|
||||
viewBindings: any[];
|
||||
|
||||
templateUrl: string;
|
||||
|
||||
template: string;
|
||||
|
||||
styleUrls: string[];
|
||||
|
||||
styles: string[];
|
||||
|
||||
directives: Array<Type | any[]>;
|
||||
|
||||
pipes: Array<Type | any[]>;
|
||||
|
||||
encapsulation: ViewEncapsulation;
|
||||
|
||||
constructor({selector, inputs, outputs, properties, events, host, exportAs, moduleId, bindings,
|
||||
viewBindings, changeDetection = ChangeDetectionStrategy.Default, queries}: {
|
||||
viewBindings, changeDetection = ChangeDetectionStrategy.Default, queries,
|
||||
templateUrl, template, styleUrls, styles, directives, pipes, encapsulation}: {
|
||||
selector?: string,
|
||||
inputs?: string[],
|
||||
outputs?: string[],
|
||||
@ -890,6 +906,13 @@ export class ComponentMetadata extends DirectiveMetadata {
|
||||
viewBindings?: any[],
|
||||
queries?: {[key: string]: any},
|
||||
changeDetection?: ChangeDetectionStrategy,
|
||||
templateUrl?: string,
|
||||
template?: string,
|
||||
styleUrls?: string[],
|
||||
styles?: string[],
|
||||
directives?: Array<Type | any[]>,
|
||||
pipes?: Array<Type | any[]>,
|
||||
encapsulation?: ViewEncapsulation
|
||||
} = {}) {
|
||||
super({
|
||||
selector: selector,
|
||||
@ -906,6 +929,14 @@ export class ComponentMetadata extends DirectiveMetadata {
|
||||
|
||||
this.changeDetection = changeDetection;
|
||||
this.viewBindings = viewBindings;
|
||||
|
||||
this.templateUrl = templateUrl;
|
||||
this.template = template;
|
||||
this.styleUrls = styleUrls;
|
||||
this.styles = styles;
|
||||
this.directives = directives;
|
||||
this.pipes = pipes;
|
||||
this.encapsulation = encapsulation;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user