feat: support decorator chaining and class creation in ES5

Closes #2534
This commit is contained in:
Misko Hevery
2015-06-12 23:51:42 -07:00
parent 4f581671dc
commit c3ae34f066
12 changed files with 371 additions and 75 deletions

View File

@ -6,6 +6,8 @@
export {
Component as ComponentAnnotation,
Directive as DirectiveAnnotation,
ComponentArgs,
DirectiveArgs,
onDestroy,
onChange,
onCheck,

View File

@ -1,5 +1,10 @@
import {ComponentAnnotation, DirectiveAnnotation} from './annotations';
import {ViewAnnotation} from './view';
import {
ComponentAnnotation,
DirectiveAnnotation,
ComponentArgs,
DirectiveArgs
} from './annotations';
import {ViewAnnotation, ViewArgs} from './view';
import {
SelfAnnotation,
ParentAnnotation,
@ -7,14 +12,39 @@ import {
UnboundedAnnotation
} from './visibility';
import {AttributeAnnotation, QueryAnnotation} from './di';
import {makeDecorator, makeParamDecorator} from '../../util/decorators';
import {makeDecorator, makeParamDecorator, TypeDecorator, Class} from '../../util/decorators';
import {Type} from 'angular2/src/facade/lang';
export interface DirectiveTypeDecorator extends TypeDecorator {}
export interface ComponentTypeDecorator extends TypeDecorator {
View(obj: ViewArgs): ViewTypeDecorator;
}
export interface ViewTypeDecorator extends TypeDecorator { View(obj: ViewArgs): ViewTypeDecorator }
export interface Directive {
(obj: any): DirectiveTypeDecorator;
new (obj: DirectiveAnnotation): DirectiveAnnotation;
}
export interface Component {
(obj: any): ComponentTypeDecorator;
new (obj: ComponentAnnotation): ComponentAnnotation;
}
export interface View {
(obj: ViewArgs): ViewTypeDecorator;
new (obj: ViewArgs): ViewAnnotation;
}
/* from annotations */
export var Component = makeDecorator(ComponentAnnotation);
export var Directive = makeDecorator(DirectiveAnnotation);
export var Component = <Component>makeDecorator(ComponentAnnotation, (fn: any) => fn.View = View);
export var Directive = <Directive>makeDecorator(DirectiveAnnotation);
/* from view */
export var View = makeDecorator(ViewAnnotation);
export var View = <View>makeDecorator(ViewAnnotation, (fn: any) => fn.View = View);
/* from visibility */
export var Self = makeParamDecorator(SelfAnnotation);

View File

@ -1,3 +1 @@
export {
View as ViewAnnotation,
} from '../annotations_impl/view';
export {View as ViewAnnotation, ViewArgs} from '../annotations_impl/view';

View File

@ -787,16 +787,7 @@ export class Directive extends Injectable {
constructor({
selector, properties, events, host, lifecycle, hostInjector, exportAs,
compileChildren = true,
}: {
selector?: string,
properties?: List<string>,
events?: List<string>,
host?: StringMap<string, string>,
lifecycle?: List<LifecycleEvent>,
hostInjector?: List<any>,
exportAs?: string,
compileChildren?: boolean
} = {}) {
}: ComponentArgs = {}) {
super();
this.selector = selector;
this.properties = properties;
@ -809,6 +800,17 @@ export class Directive extends Injectable {
}
}
export interface ComponentArgs {
selector?: string;
properties?: List<string>;
events?: List<string>;
host?: StringMap<string, string>;
lifecycle?: List<LifecycleEvent>;
hostInjector?: List<any>;
exportAs?: string;
compileChildren?: boolean;
}
/**
* Declare reusable UI building blocks for an application.
*
@ -1007,19 +1009,8 @@ export class Component extends Directive {
viewInjector: List<any>;
constructor({selector, properties, events, host, exportAs, appInjector, lifecycle, hostInjector,
viewInjector, changeDetection = DEFAULT, compileChildren = true}: {
selector?: string,
properties?: List<string>,
events?: List<string>,
host?: StringMap<string, string>,
exportAs?: string,
appInjector?: List<any>,
lifecycle?: List<LifecycleEvent>,
hostInjector?: List<any>,
viewInjector?: List<any>,
changeDetection?: string,
compileChildren?: boolean
} = {}) {
viewInjector, changeDetection = DEFAULT,
compileChildren = true}: DirectiveArgs = {}) {
super({
selector: selector,
properties: properties,
@ -1036,6 +1027,20 @@ export class Component extends Directive {
this.viewInjector = viewInjector;
}
}
export interface DirectiveArgs {
selector?: string;
properties?: List<string>;
events?: List<string>;
host?: StringMap<string, string>;
exportAs?: string;
appInjector?: List<any>;
lifecycle?: List<LifecycleEvent>;
hostInjector?: List<any>;
viewInjector?: List<any>;
changeDetection?: string;
compileChildren?: boolean;
}
/**
* Lifecycle events are guaranteed to be called in the following order:

View File

@ -82,15 +82,16 @@ export class View {
*/
renderer: string;
constructor({templateUrl, template, directives, renderer}: {
templateUrl?: string,
template?: string,
directives?: List<Type | any | List<any>>,
renderer?: string
} = {}) {
constructor({templateUrl, template, directives, renderer}: ViewArgs = {}) {
this.templateUrl = templateUrl;
this.template = template;
this.directives = directives;
this.renderer = renderer;
}
}
export interface ViewArgs {
templateUrl?: string;
template?: string;
directives?: List<Type | any | List<any>>;
renderer?: string;
}