Skip to content

Commit f210c41

Browse files
committed
feat(di): changed toFactory to support dependency annotations
1 parent 863eb3c commit f210c41

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

modules/angular2/src/di/binding.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,7 @@ export class BindingBuilder {
431431
function _constructDependencies(factoryFunction: Function, dependencies: List<any>) {
432432
return isBlank(dependencies) ?
433433
_dependenciesFor(factoryFunction) :
434-
ListWrapper.map(dependencies,
435-
(t) => Dependency.fromKey(Key.get(resolveForwardRef(t))));
434+
ListWrapper.map(dependencies, (t) => _extractToken(factoryFunction, t));
436435
}
437436

438437
function _dependenciesFor(typeOrFunc): List<any> {
@@ -451,6 +450,10 @@ function _extractToken(typeOrFunc, annotations) {
451450
var lazy = false;
452451
var asPromise = false;
453452

453+
if (!ListWrapper.isList(annotations)) {
454+
return _createDependency(annotations, asPromise, lazy, optional, depProps);
455+
}
456+
454457
for (var i = 0; i < annotations.length; ++i) {
455458
var paramAnnotation = annotations[i];
456459

modules/angular2/test/di/injector_spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {isBlank, BaseException} from 'angular2/src/facade/lang';
22
import {describe, ddescribe, it, iit, expect, beforeEach} from 'angular2/test_lib';
3-
import {Injector, bind, ResolvedBinding, Key, forwardRef} from 'angular2/di';
3+
import {Injector, bind, ResolvedBinding, Key, forwardRef, DependencyAnnotation} from 'angular2/di';
44
import {Optional, Inject, InjectLazy} from 'angular2/src/di/annotations_impl';
55

6+
class CustomDependencyAnnotation extends DependencyAnnotation {
7+
}
68

79
class Engine {
810
}
@@ -408,6 +410,16 @@ export function main() {
408410
expect(stringBinding.dependencies[0].key).toEqual(Key.get(Engine));
409411
expect(dashboardSoftwareBinding.dependencies[0].key).toEqual(Key.get(BrokenEngine));
410412
});
413+
414+
it('should support overriding factory dependencies with dependency annotations', function () {
415+
var bindings = Injector.resolve([
416+
bind("token").toFactory((e) => "result", [[new Inject("dep"), new CustomDependencyAnnotation()]])
417+
]);
418+
var binding = bindings[Key.get("token").id];
419+
420+
expect(binding.dependencies[0].key).toEqual(Key.get("dep"));
421+
expect(binding.dependencies[0].properties).toEqual([new CustomDependencyAnnotation()]);
422+
});
411423
});
412424
});
413425
}

0 commit comments

Comments
 (0)