feat(decorators): adds support for parameter decorators.
Paramater decorators expect to be called as currently implemented by TS.
This commit is contained in:
@ -5,12 +5,15 @@ import {
|
||||
import {ViewAnnotation} from '../annotations/view';
|
||||
import {AncestorAnnotation, ParentAnnotation} from '../annotations/visibility';
|
||||
import {AttributeAnnotation, QueryAnnotation} from '../annotations/di';
|
||||
import {global} from 'angular2/src/facade/lang';
|
||||
|
||||
function makeDecorator(annotationCls) {
|
||||
return function(...args) {
|
||||
if (!(window.Reflect && window.Reflect.getMetadata)) throw 'reflect-metadata shim is required';
|
||||
var Reflect = global.Reflect;
|
||||
if (!(Reflect && Reflect.getMetadata)) {
|
||||
throw 'reflect-metadata shim is required when using class decorators';
|
||||
}
|
||||
var annotationInstance = new annotationCls(...args);
|
||||
var Reflect = window.Reflect;
|
||||
return function(cls) {
|
||||
var annotations = Reflect.getMetadata('annotations', cls);
|
||||
annotations = annotations || [];
|
||||
@ -21,17 +24,39 @@ function makeDecorator(annotationCls) {
|
||||
}
|
||||
}
|
||||
|
||||
function makeParamDecorator(annotationCls) {
|
||||
return function(...args) {
|
||||
var Reflect = global.Reflect;
|
||||
if (!(Reflect && Reflect.getMetadata)) {
|
||||
throw 'reflect-metadata shim is required when using parameter decorators';
|
||||
}
|
||||
var annotationInstance = new annotationCls(...args);
|
||||
return function(cls, unusedKey, index) {
|
||||
var parameters = Reflect.getMetadata('parameters', cls);
|
||||
parameters = parameters || [];
|
||||
// there might be gaps if some in between parameters do not have annotations.
|
||||
// we pad with nulls.
|
||||
while (parameters.length <= index) {
|
||||
parameters.push(null);
|
||||
}
|
||||
parameters[index] = annotationInstance;
|
||||
Reflect.defineMetadata('parameters', parameters, cls);
|
||||
return cls;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* from annotations */
|
||||
export var Component = makeDecorator(ComponentAnnotation);
|
||||
export var Decorator = makeDecorator(DirectiveAnnotation);
|
||||
|
||||
/* from di */
|
||||
export var Attribute = makeDecorator(AttributeAnnotation);
|
||||
export var Query = makeDecorator(QueryAnnotation);
|
||||
|
||||
/* from view */
|
||||
export var View = makeDecorator(ViewAnnotation);
|
||||
|
||||
/* from visiblity */
|
||||
export var Ancestor = makeDecorator(AncestorAnnotation);
|
||||
export var Parent = makeDecorator(ParentAnnotation);
|
||||
/* from visibility */
|
||||
export var Ancestor = makeParamDecorator(AncestorAnnotation);
|
||||
export var Parent = makeParamDecorator(ParentAnnotation);
|
||||
|
||||
/* from di */
|
||||
export var Attribute = makeParamDecorator(AttributeAnnotation);
|
||||
export var Query = makeParamDecorator(QueryAnnotation);
|
||||
|
Reference in New Issue
Block a user