From f210c41c1fe00324f6962219a61f69ecb472b7da Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 21 May 2015 08:34:48 -0700 Subject: [PATCH] feat(di): changed toFactory to support dependency annotations --- modules/angular2/src/di/binding.ts | 7 +++++-- modules/angular2/test/di/injector_spec.js | 14 +++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/di/binding.ts b/modules/angular2/src/di/binding.ts index cd622a1a2a..a3cbecce82 100644 --- a/modules/angular2/src/di/binding.ts +++ b/modules/angular2/src/di/binding.ts @@ -431,8 +431,7 @@ export class BindingBuilder { function _constructDependencies(factoryFunction: Function, dependencies: List) { return isBlank(dependencies) ? _dependenciesFor(factoryFunction) : - ListWrapper.map(dependencies, - (t) => Dependency.fromKey(Key.get(resolveForwardRef(t)))); + ListWrapper.map(dependencies, (t) => _extractToken(factoryFunction, t)); } function _dependenciesFor(typeOrFunc): List { @@ -451,6 +450,10 @@ function _extractToken(typeOrFunc, annotations) { var lazy = false; var asPromise = false; + if (!ListWrapper.isList(annotations)) { + return _createDependency(annotations, asPromise, lazy, optional, depProps); + } + for (var i = 0; i < annotations.length; ++i) { var paramAnnotation = annotations[i]; diff --git a/modules/angular2/test/di/injector_spec.js b/modules/angular2/test/di/injector_spec.js index 93daaacd95..6b26a21c90 100644 --- a/modules/angular2/test/di/injector_spec.js +++ b/modules/angular2/test/di/injector_spec.js @@ -1,8 +1,10 @@ import {isBlank, BaseException} from 'angular2/src/facade/lang'; import {describe, ddescribe, it, iit, expect, beforeEach} from 'angular2/test_lib'; -import {Injector, bind, ResolvedBinding, Key, forwardRef} from 'angular2/di'; +import {Injector, bind, ResolvedBinding, Key, forwardRef, DependencyAnnotation} from 'angular2/di'; import {Optional, Inject, InjectLazy} from 'angular2/src/di/annotations_impl'; +class CustomDependencyAnnotation extends DependencyAnnotation { +} class Engine { } @@ -408,6 +410,16 @@ export function main() { expect(stringBinding.dependencies[0].key).toEqual(Key.get(Engine)); expect(dashboardSoftwareBinding.dependencies[0].key).toEqual(Key.get(BrokenEngine)); }); + + it('should support overriding factory dependencies with dependency annotations', function () { + var bindings = Injector.resolve([ + bind("token").toFactory((e) => "result", [[new Inject("dep"), new CustomDependencyAnnotation()]]) + ]); + var binding = bindings[Key.get("token").id]; + + expect(binding.dependencies[0].key).toEqual(Key.get("dep")); + expect(binding.dependencies[0].properties).toEqual([new CustomDependencyAnnotation()]); + }); }); }); }