feat(decorators): adds decorator versions of DI annotations.
In 'angular2/di' the symbol: - Inject is a decorator - InjectAnnotation is an annotation Internally one an get a hold of annotations without *Annotations appened (to make ts2dart work without workarounds) by importing from 'angular2/src/di/annotations_impl' instead of 'angular2/di'. This is needed only for users that transpile through TS and through ts2dart.
This commit is contained in:
40
modules/angular2/src/util/decorators.es6
Normal file
40
modules/angular2/src/util/decorators.es6
Normal file
@ -0,0 +1,40 @@
|
||||
import {global} from 'angular2/src/facade/lang';
|
||||
|
||||
export function makeDecorator(annotationCls) {
|
||||
return function(...args) {
|
||||
var Reflect = global.Reflect;
|
||||
if (!(Reflect && Reflect.getMetadata)) {
|
||||
throw 'reflect-metadata shim is required when using class decorators';
|
||||
}
|
||||
var annotationInstance = new annotationCls(...args);
|
||||
return function(cls) {
|
||||
var annotations = Reflect.getMetadata('annotations', cls);
|
||||
annotations = annotations || [];
|
||||
annotations.push(annotationInstance);
|
||||
Reflect.defineMetadata('annotations', annotations, cls);
|
||||
return cls;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user