Skip to content

Commit 705ee46

Browse files
committed
fix(di): changed host and view injector to respect visibility
1 parent f210c41 commit 705ee46

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

modules/angular2/src/core/compiler/element_injector.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,28 @@ export class TreeNode<T extends TreeNode<any>> {
189189
}
190190
}
191191

192-
export class DirectiveDependency extends Dependency {
192+
export class DependencyWithVisibility extends Dependency {
193193
constructor(key: Key, asPromise: boolean, lazy: boolean, optional: boolean, properties: List<any>,
194-
public visibility: Visibility, public attributeName: string, public queryDirective) {
194+
public visibility: Visibility) {
195195
super(key, asPromise, lazy, optional, properties);
196+
}
197+
198+
static createFrom(d: Dependency): Dependency {
199+
return new DependencyWithVisibility(d.key, d.asPromise, d.lazy, d.optional, d.properties,
200+
DependencyWithVisibility._visibility(d.properties));
201+
}
202+
203+
static _visibility(properties): Visibility {
204+
if (properties.length == 0) return self;
205+
var p = ListWrapper.find(properties, p => p instanceof Visibility);
206+
return isPresent(p) ? p : self;
207+
}
208+
}
209+
210+
export class DirectiveDependency extends DependencyWithVisibility {
211+
constructor(key: Key, asPromise: boolean, lazy: boolean, optional: boolean, properties: List<any>,
212+
visibility: Visibility, public attributeName: string, public queryDirective) {
213+
super(key, asPromise, lazy, optional, properties, visibility);
196214
this._verify();
197215
}
198216

@@ -207,17 +225,11 @@ export class DirectiveDependency extends Dependency {
207225

208226
static createFrom(d: Dependency): Dependency {
209227
return new DirectiveDependency(d.key, d.asPromise, d.lazy, d.optional, d.properties,
210-
DirectiveDependency._visibility(d.properties),
228+
DependencyWithVisibility._visibility(d.properties),
211229
DirectiveDependency._attributeName(d.properties),
212230
DirectiveDependency._query(d.properties));
213231
}
214232

215-
static _visibility(properties): Visibility {
216-
if (properties.length == 0) return self;
217-
var p = ListWrapper.find(properties, p => p instanceof Visibility);
218-
return isPresent(p) ? p : self;
219-
}
220-
221233
static _attributeName(properties): string {
222234
var p = ListWrapper.find(properties, (p) => p instanceof Attribute);
223235
return isPresent(p) ? p.attributeName : null;
@@ -476,16 +488,24 @@ export class ProtoElementInjector {
476488
private static _createHostInjectorBindingData(bindings: List<ResolvedBinding>,
477489
bd: List<BindingData>) {
478490
ListWrapper.forEach(bindings, b => {
479-
ListWrapper.forEach(b.resolvedHostInjectables,
480-
b => { ListWrapper.push(bd, new BindingData(b, LIGHT_DOM)); });
491+
ListWrapper.forEach(b.resolvedHostInjectables, b => {
492+
ListWrapper.push(bd, new BindingData(ProtoElementInjector._createBinding(b), LIGHT_DOM));
493+
});
481494
});
482495
}
483496

484497
private static _createViewInjectorBindingData(bindings: List<ResolvedBinding>,
485498
bd: List<BindingData>) {
486499
var db = <DirectiveBinding>bindings[0];
487-
ListWrapper.forEach(db.resolvedViewInjectables,
488-
b => ListWrapper.push(bd, new BindingData(b, SHADOW_DOM)));
500+
ListWrapper.forEach(
501+
db.resolvedViewInjectables,
502+
b => ListWrapper.push(bd,
503+
new BindingData(ProtoElementInjector._createBinding(b), SHADOW_DOM)));
504+
}
505+
506+
private static _createBinding(b: ResolvedBinding) {
507+
var deps = ListWrapper.map(b.dependencies, d => DependencyWithVisibility.createFrom(d));
508+
return new ResolvedBinding(b.key, b.factory, deps, b.providedAsPromise);
489509
}
490510

491511
constructor(parent: ProtoElementInjector, index: int, bd: List<BindingData>,
@@ -909,9 +929,9 @@ export class ElementInjector extends TreeNode<ElementInjector> {
909929
return obj;
910930
}
911931

912-
private _getByDependency(dep: Dependency, requestor: Key) {
932+
private _getByDependency(dep: DependencyWithVisibility, requestor: Key) {
913933
if (!(dep instanceof DirectiveDependency)) {
914-
return this._getByKey(dep.key, self, dep.optional, requestor);
934+
return this._getByKey(dep.key, dep.visibility, dep.optional, requestor);
915935
}
916936

917937
var dirDep = <DirectiveDependency>dep;

modules/angular2/src/di/binding.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,5 +492,5 @@ function _extractToken(typeOrFunc, annotations) {
492492
}
493493

494494
function _createDependency(token, asPromise, lazy, optional, depProps): Dependency {
495-
return new Dependency(Key.get(token), asPromise, lazy, optional, depProps);
495+
return new Dependency(Key.get(resolveForwardRef(token)), asPromise, lazy, optional, depProps);
496496
}

modules/angular2/test/core/compiler/element_injector_spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {ProtoElementInjector, ElementInjector, PreBuiltObjects, DirectiveBinding
66
import {Parent, Ancestor, Unbounded} from 'angular2/src/core/annotations_impl/visibility';
77
import {Attribute, Query} from 'angular2/src/core/annotations_impl/di';
88
import {Component, Directive, onDestroy} from 'angular2/src/core/annotations_impl/annotations';
9-
import {bind, Injector, Binding} from 'angular2/di';
9+
import {bind, Injector, Binding, resolveBindings} from 'angular2/di';
1010
import {Optional, Inject} from 'angular2/src/di/annotations_impl';
1111
import {AppProtoView, AppView} from 'angular2/src/core/compiler/view';
1212
import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref';
@@ -528,6 +528,22 @@ export function main() {
528528
expect(inj.get('injectable2')).toEqual('injectable1-injectable2');
529529
});
530530

531+
it("should instantiate hostInjector injectables that have dependencies with set visibility", function () {
532+
var childInj= parentChildInjectors([
533+
DirectiveBinding.createFromType(SimpleDirective,
534+
new DummyDirective({hostInjector: [
535+
bind('injectable1').toValue('injectable1')
536+
]}))
537+
], [
538+
DirectiveBinding.createFromType(SimpleDirective,
539+
new DummyDirective({hostInjector: [
540+
bind('injectable1').toValue('new-injectable1'),
541+
bind('injectable2').toFactory((val) => `${val}-injectable2`, [[new Inject('injectable1'), new Parent()]])
542+
]}))
543+
]);
544+
expect(childInj.get('injectable2')).toEqual('injectable1-injectable2');
545+
});
546+
531547
it("should instantiate components that depends on viewInjector dependencies", function () {
532548
var inj = injector([
533549
DirectiveBinding.createFromType(NeedsService,

0 commit comments

Comments
 (0)