From 0ed5dd0d7b7fd0c550783c207da047ae94a4c8c7 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 9 Jul 2015 11:40:09 -0700 Subject: [PATCH] fix(di): hostInjector and viewInjector support nested arrays --- modules/angular2/di.ts | 1 - .../src/core/compiler/element_injector.ts | 5 ++--- .../src/core/compiler/proto_view_factory.ts | 1 - modules/angular2/src/di/injector.ts | 6 +++--- .../test/core/compiler/element_injector_spec.ts | 17 ++++++++++++++++- .../test/core/compiler/integration_spec.ts | 6 +++--- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/modules/angular2/di.ts b/modules/angular2/di.ts index 1c338220cf..3815136c9b 100644 --- a/modules/angular2/di.ts +++ b/modules/angular2/di.ts @@ -24,7 +24,6 @@ export * from './src/di/decorators'; export {forwardRef, resolveForwardRef, ForwardRefFn} from './src/di/forward_ref'; export { - resolveBindings, Injector, ProtoInjector, DependencyProvider, diff --git a/modules/angular2/src/core/compiler/element_injector.ts b/modules/angular2/src/core/compiler/element_injector.ts index b8a3375aa8..130b7556c1 100644 --- a/modules/angular2/src/core/compiler/element_injector.ts +++ b/modules/angular2/src/core/compiler/element_injector.ts @@ -25,7 +25,6 @@ import { AbstractBindingError, CyclicDependencyError, resolveForwardRef, - resolveBindings, VisibilityMetadata, DependencyProvider, self @@ -240,9 +239,9 @@ export class DirectiveBinding extends ResolvedBinding { var rb = binding.resolve(); var deps = ListWrapper.map(rb.dependencies, DirectiveDependency.createFrom); var resolvedHostInjectables = - isPresent(ann.hostInjector) ? resolveBindings(ann.hostInjector) : []; + isPresent(ann.hostInjector) ? Injector.resolve(ann.hostInjector) : []; var resolvedViewInjectables = ann instanceof Component && isPresent(ann.viewInjector) ? - resolveBindings(ann.viewInjector) : + Injector.resolve(ann.viewInjector) : []; var metadata = DirectiveMetadata.create({ id: stringify(rb.key.token), diff --git a/modules/angular2/src/core/compiler/proto_view_factory.ts b/modules/angular2/src/core/compiler/proto_view_factory.ts index 226871874b..abcf140b8e 100644 --- a/modules/angular2/src/core/compiler/proto_view_factory.ts +++ b/modules/angular2/src/core/compiler/proto_view_factory.ts @@ -364,7 +364,6 @@ function _createProtoElementInjector(binderIndex, parentPeiWithDistance, renderE if (directiveBindings.length > 0 || hasVariables) { var directiveVariableBindings = createDirectiveVariableBindings(renderElementBinder, directiveBindings); - protoElementInjector = ProtoElementInjector.create(parentPeiWithDistance.protoElementInjector, binderIndex, directiveBindings, isPresent(componentDirectiveBinding), diff --git a/modules/angular2/src/di/injector.ts b/modules/angular2/src/di/injector.ts index 2c534b027e..0110d233a5 100644 --- a/modules/angular2/src/di/injector.ts +++ b/modules/angular2/src/di/injector.ts @@ -467,7 +467,7 @@ export class Injector { * `fromResolvedBindings` and `createChildFromResolved`. */ static resolve(bindings: List>): List { - var resolvedBindings = resolveBindings(bindings); + var resolvedBindings = _resolveBindings(bindings); var flatten = _flattenBindings(resolvedBindings, new Map()); return _createListOfBindings(flatten); } @@ -765,7 +765,7 @@ export class Injector { } -export function resolveBindings(bindings: List>): List { +function _resolveBindings(bindings: List>): List { var resolvedList = ListWrapper.createFixedSize(bindings.length); for (var i = 0; i < bindings.length; i++) { var unresolved = resolveForwardRef(bindings[i]); @@ -777,7 +777,7 @@ export function resolveBindings(bindings: List>): Lis } else if (unresolved instanceof Binding) { resolved = unresolved.resolve(); } else if (unresolved instanceof List) { - resolved = resolveBindings(unresolved); + resolved = _resolveBindings(unresolved); } else if (unresolved instanceof BindingBuilder) { throw new InvalidBindingError(unresolved.token); } else { diff --git a/modules/angular2/test/core/compiler/element_injector_spec.ts b/modules/angular2/test/core/compiler/element_injector_spec.ts index e5cc39b96c..63678ec93c 100644 --- a/modules/angular2/test/core/compiler/element_injector_spec.ts +++ b/modules/angular2/test/core/compiler/element_injector_spec.ts @@ -40,7 +40,7 @@ import { Directive, onDestroy } from 'angular2/annotations'; -import {bind, Injector, Binding, resolveBindings, Optional, Inject, Injectable, Self, Parent, Ancestor, Unbounded, self, InjectMetadata, ParentMetadata} from 'angular2/di'; +import {bind, Injector, Binding, Optional, Inject, Injectable, Self, Parent, Ancestor, Unbounded, self, InjectMetadata, ParentMetadata} from 'angular2/di'; import {AppProtoView, AppView} from 'angular2/src/core/compiler/view'; import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref'; import {ProtoViewRef} from 'angular2/src/core/compiler/view_ref'; @@ -486,6 +486,21 @@ export function main() { expect(pei.getBindingAtIndex(1).key.token).toEqual("injectable1"); }); + it("should collect view and host injectables from nested arrays", () => { + var pei = createPei(null, 0, [ + DirectiveBinding.createFromType( + SimpleDirective, + new dirAnn.Component({ + viewInjector: [[[bind('view').toValue('view')]]], + hostInjector: [[[bind('host').toValue('host')]]] + })) + ], 0, true); + + expect(pei.getBindingAtIndex(0).key.token).toBe(SimpleDirective); + expect(pei.getBindingAtIndex(1).key.token).toEqual("view"); + expect(pei.getBindingAtIndex(2).key.token).toEqual("host"); + }); + it('should support an arbitrary number of bindings', () => { var pei = createPei(null, 0, dynamicBindings); diff --git a/modules/angular2/test/core/compiler/integration_spec.ts b/modules/angular2/test/core/compiler/integration_spec.ts index 40e187187c..7f7ac4161e 100644 --- a/modules/angular2/test/core/compiler/integration_spec.ts +++ b/modules/angular2/test/core/compiler/integration_spec.ts @@ -1007,7 +1007,7 @@ export function main() { inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { tcb.overrideView(MyComp, new viewAnn.View({ template: ` - + @@ -1697,12 +1697,12 @@ class DirectiveWithTwoWayBinding { class InjectableService { } -@Directive({selector: 'directive-providing-injectable', hostInjector: [InjectableService]}) +@Directive({selector: 'directive-providing-injectable', hostInjector: [[InjectableService]]}) @Injectable() class DirectiveProvidingInjectable { } -@Component({selector: 'directive-providing-injectable', viewInjector: [InjectableService]}) +@Component({selector: 'directive-providing-injectable', viewInjector: [[InjectableService]]}) @View({template: ''}) @Injectable() class DirectiveProvidingInjectableInView {