@Inject decorator for easily annotating dependency-injected class constructors
This commit is contained in:
parent
eea2a18c3f
commit
edd7314624
5 changed files with 33 additions and 4 deletions
17
static/js/decorators/inject/inject.decorator.spec.ts
Normal file
17
static/js/decorators/inject/inject.decorator.spec.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import { Inject } from './inject.decorator';
|
||||||
|
|
||||||
|
|
||||||
|
describe("Decorator: Inject", () => {
|
||||||
|
|
||||||
|
describe("parameter injection", () => {
|
||||||
|
|
||||||
|
it("adds given string to the 'inject' property of the annotated class", () => {
|
||||||
|
expect(ValidService.$inject).toContain('$scope');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
class ValidService {
|
||||||
|
constructor(@Inject('$scope') private $scope: any) {}
|
||||||
|
}
|
11
static/js/decorators/inject/inject.decorator.ts
Normal file
11
static/js/decorators/inject/inject.decorator.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/**
|
||||||
|
* Adds the given value to the inject property of the annotated class.
|
||||||
|
* Used to annotate the constructor parameters of an AngularJS service/component class.
|
||||||
|
* @param value The string name of the dependency.
|
||||||
|
*/
|
||||||
|
export function Inject(value: string) {
|
||||||
|
return (target: any, propertyKey: string | symbol, parameterIndex: number): void => {
|
||||||
|
target.$inject = target.$inject || [];
|
||||||
|
target.$inject[parameterIndex] = value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
import { ViewArray } from './view-array';
|
import { ViewArray } from './view-array';
|
||||||
import { ViewArrayImpl } from './view-array.impl';
|
import { ViewArrayImpl } from './view-array.impl';
|
||||||
|
import { Inject } from '../../decorators/inject/inject.decorator';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,8 +14,7 @@ export class ViewArrayFactory {
|
||||||
* @param $interval Angular $interval service.
|
* @param $interval Angular $interval service.
|
||||||
* @return viewArrayFactory A factory for creating ViewArray objects.
|
* @return viewArrayFactory A factory for creating ViewArray objects.
|
||||||
*/
|
*/
|
||||||
static $inject = ['$interval'];
|
constructor(@Inject('$interval') private $interval: ng.IIntervalService) {
|
||||||
constructor(private $interval: ng.IIntervalService) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,13 +6,13 @@ describe("ViewArrayImplImpl", () => {
|
||||||
var $intervalMock: any;
|
var $intervalMock: any;
|
||||||
var additionalCount: number;
|
var additionalCount: number;
|
||||||
|
|
||||||
beforeEach(inject(($injector: ng.auto.IInjectorService) => {
|
beforeEach(() => {
|
||||||
$intervalMock = jasmine.createSpy('$intervalSpy');
|
$intervalMock = jasmine.createSpy('$intervalSpy');
|
||||||
$intervalMock.and.returnValue({});
|
$intervalMock.and.returnValue({});
|
||||||
$intervalMock.cancel = jasmine.createSpy('cancelSpy');
|
$intervalMock.cancel = jasmine.createSpy('cancelSpy');
|
||||||
additionalCount = 20;
|
additionalCount = 20;
|
||||||
viewArrayImpl = new ViewArrayImpl($intervalMock, additionalCount);
|
viewArrayImpl = new ViewArrayImpl($intervalMock, additionalCount);
|
||||||
}));
|
});
|
||||||
|
|
||||||
|
|
||||||
describe("constructor", () => {
|
describe("constructor", () => {
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"outDir": "./build/",
|
"outDir": "./build/",
|
||||||
"target": "es5",
|
"target": "es5",
|
||||||
|
"experimentalDecorators": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"paths": {
|
"paths": {
|
||||||
"sass/*": ["./static/css/directives/components/pages/*"]
|
"sass/*": ["./static/css/directives/components/pages/*"]
|
||||||
|
|
Reference in a new issue