From edd7314624a1b65516947276ab32e5a5bdc99ba7 Mon Sep 17 00:00:00 2001 From: alecmerdler Date: Sat, 21 Jan 2017 02:56:29 -0800 Subject: [PATCH] @Inject decorator for easily annotating dependency-injected class constructors --- .../decorators/inject/inject.decorator.spec.ts | 17 +++++++++++++++++ static/js/decorators/inject/inject.decorator.ts | 11 +++++++++++ .../services/view-array/view-array.factory.ts | 4 ++-- .../services/view-array/view-array.impl.spec.ts | 4 ++-- tsconfig.json | 1 + 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 static/js/decorators/inject/inject.decorator.spec.ts create mode 100644 static/js/decorators/inject/inject.decorator.ts diff --git a/static/js/decorators/inject/inject.decorator.spec.ts b/static/js/decorators/inject/inject.decorator.spec.ts new file mode 100644 index 000000000..2f0a9a17b --- /dev/null +++ b/static/js/decorators/inject/inject.decorator.spec.ts @@ -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) {} +} \ No newline at end of file diff --git a/static/js/decorators/inject/inject.decorator.ts b/static/js/decorators/inject/inject.decorator.ts new file mode 100644 index 000000000..2c647a4c7 --- /dev/null +++ b/static/js/decorators/inject/inject.decorator.ts @@ -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; + } +} diff --git a/static/js/services/view-array/view-array.factory.ts b/static/js/services/view-array/view-array.factory.ts index 0a4a4de2d..99601e759 100644 --- a/static/js/services/view-array/view-array.factory.ts +++ b/static/js/services/view-array/view-array.factory.ts @@ -1,5 +1,6 @@ import { ViewArray } from './view-array'; 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. * @return viewArrayFactory A factory for creating ViewArray objects. */ - static $inject = ['$interval']; - constructor(private $interval: ng.IIntervalService) { + constructor(@Inject('$interval') private $interval: ng.IIntervalService) { } diff --git a/static/js/services/view-array/view-array.impl.spec.ts b/static/js/services/view-array/view-array.impl.spec.ts index 82128e5bf..ae9ee113b 100644 --- a/static/js/services/view-array/view-array.impl.spec.ts +++ b/static/js/services/view-array/view-array.impl.spec.ts @@ -6,13 +6,13 @@ describe("ViewArrayImplImpl", () => { var $intervalMock: any; var additionalCount: number; - beforeEach(inject(($injector: ng.auto.IInjectorService) => { + beforeEach(() => { $intervalMock = jasmine.createSpy('$intervalSpy'); $intervalMock.and.returnValue({}); $intervalMock.cancel = jasmine.createSpy('cancelSpy'); additionalCount = 20; viewArrayImpl = new ViewArrayImpl($intervalMock, additionalCount); - })); + }); describe("constructor", () => { diff --git a/tsconfig.json b/tsconfig.json index 2e7ab719c..9ec73f044 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "module": "commonjs", "outDir": "./build/", "target": "es5", + "experimentalDecorators": true, "sourceMap": true, "paths": { "sass/*": ["./static/css/directives/components/pages/*"]