fix(di): hostInjector and viewInjector support nested arrays

This commit is contained in:
vsavkin 2015-07-09 11:40:09 -07:00
parent b716046b97
commit 0ed5dd0d7b
6 changed files with 24 additions and 12 deletions

View File

@ -24,7 +24,6 @@ export * from './src/di/decorators';
export {forwardRef, resolveForwardRef, ForwardRefFn} from './src/di/forward_ref'; export {forwardRef, resolveForwardRef, ForwardRefFn} from './src/di/forward_ref';
export { export {
resolveBindings,
Injector, Injector,
ProtoInjector, ProtoInjector,
DependencyProvider, DependencyProvider,

View File

@ -25,7 +25,6 @@ import {
AbstractBindingError, AbstractBindingError,
CyclicDependencyError, CyclicDependencyError,
resolveForwardRef, resolveForwardRef,
resolveBindings,
VisibilityMetadata, VisibilityMetadata,
DependencyProvider, DependencyProvider,
self self
@ -240,9 +239,9 @@ export class DirectiveBinding extends ResolvedBinding {
var rb = binding.resolve(); var rb = binding.resolve();
var deps = ListWrapper.map(rb.dependencies, DirectiveDependency.createFrom); var deps = ListWrapper.map(rb.dependencies, DirectiveDependency.createFrom);
var resolvedHostInjectables = var resolvedHostInjectables =
isPresent(ann.hostInjector) ? resolveBindings(ann.hostInjector) : []; isPresent(ann.hostInjector) ? Injector.resolve(ann.hostInjector) : [];
var resolvedViewInjectables = ann instanceof Component && isPresent(ann.viewInjector) ? var resolvedViewInjectables = ann instanceof Component && isPresent(ann.viewInjector) ?
resolveBindings(ann.viewInjector) : Injector.resolve(ann.viewInjector) :
[]; [];
var metadata = DirectiveMetadata.create({ var metadata = DirectiveMetadata.create({
id: stringify(rb.key.token), id: stringify(rb.key.token),

View File

@ -364,7 +364,6 @@ function _createProtoElementInjector(binderIndex, parentPeiWithDistance, renderE
if (directiveBindings.length > 0 || hasVariables) { if (directiveBindings.length > 0 || hasVariables) {
var directiveVariableBindings = var directiveVariableBindings =
createDirectiveVariableBindings(renderElementBinder, directiveBindings); createDirectiveVariableBindings(renderElementBinder, directiveBindings);
protoElementInjector = protoElementInjector =
ProtoElementInjector.create(parentPeiWithDistance.protoElementInjector, binderIndex, ProtoElementInjector.create(parentPeiWithDistance.protoElementInjector, binderIndex,
directiveBindings, isPresent(componentDirectiveBinding), directiveBindings, isPresent(componentDirectiveBinding),

View File

@ -467,7 +467,7 @@ export class Injector {
* `fromResolvedBindings` and `createChildFromResolved`. * `fromResolvedBindings` and `createChildFromResolved`.
*/ */
static resolve(bindings: List<Type | Binding | List<any>>): List<ResolvedBinding> { static resolve(bindings: List<Type | Binding | List<any>>): List<ResolvedBinding> {
var resolvedBindings = resolveBindings(bindings); var resolvedBindings = _resolveBindings(bindings);
var flatten = _flattenBindings(resolvedBindings, new Map()); var flatten = _flattenBindings(resolvedBindings, new Map());
return _createListOfBindings(flatten); return _createListOfBindings(flatten);
} }
@ -765,7 +765,7 @@ export class Injector {
} }
export function resolveBindings(bindings: List<Type | Binding | List<any>>): List<ResolvedBinding> { function _resolveBindings(bindings: List<Type | Binding | List<any>>): List<ResolvedBinding> {
var resolvedList = ListWrapper.createFixedSize(bindings.length); var resolvedList = ListWrapper.createFixedSize(bindings.length);
for (var i = 0; i < bindings.length; i++) { for (var i = 0; i < bindings.length; i++) {
var unresolved = resolveForwardRef(bindings[i]); var unresolved = resolveForwardRef(bindings[i]);
@ -777,7 +777,7 @@ export function resolveBindings(bindings: List<Type | Binding | List<any>>): Lis
} else if (unresolved instanceof Binding) { } else if (unresolved instanceof Binding) {
resolved = unresolved.resolve(); resolved = unresolved.resolve();
} else if (unresolved instanceof List) { } else if (unresolved instanceof List) {
resolved = resolveBindings(unresolved); resolved = _resolveBindings(unresolved);
} else if (unresolved instanceof BindingBuilder) { } else if (unresolved instanceof BindingBuilder) {
throw new InvalidBindingError(unresolved.token); throw new InvalidBindingError(unresolved.token);
} else { } else {

View File

@ -40,7 +40,7 @@ import {
Directive, Directive,
onDestroy onDestroy
} from 'angular2/annotations'; } 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 {AppProtoView, AppView} from 'angular2/src/core/compiler/view';
import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref'; import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref';
import {ProtoViewRef} from 'angular2/src/core/compiler/view_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"); 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', () => { it('should support an arbitrary number of bindings', () => {
var pei = createPei(null, 0, dynamicBindings); var pei = createPei(null, 0, dynamicBindings);

View File

@ -1007,7 +1007,7 @@ export function main() {
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
tcb.overrideView(MyComp, new viewAnn.View({ tcb.overrideView(MyComp, new viewAnn.View({
template: ` template: `
<directive-providing-injectable> <directive-providing-injectable >
<directive-consuming-injectable #consuming> <directive-consuming-injectable #consuming>
</directive-consuming-injectable> </directive-consuming-injectable>
</directive-providing-injectable> </directive-providing-injectable>
@ -1697,12 +1697,12 @@ class DirectiveWithTwoWayBinding {
class InjectableService { class InjectableService {
} }
@Directive({selector: 'directive-providing-injectable', hostInjector: [InjectableService]}) @Directive({selector: 'directive-providing-injectable', hostInjector: [[InjectableService]]})
@Injectable() @Injectable()
class DirectiveProvidingInjectable { class DirectiveProvidingInjectable {
} }
@Component({selector: 'directive-providing-injectable', viewInjector: [InjectableService]}) @Component({selector: 'directive-providing-injectable', viewInjector: [[InjectableService]]})
@View({template: ''}) @View({template: ''})
@Injectable() @Injectable()
class DirectiveProvidingInjectableInView { class DirectiveProvidingInjectableInView {