fix(element_injector): fixed element injector to inject view dependencies into its components

This commit is contained in:
vsavkin 2015-05-20 15:36:36 -07:00
parent dd9b08cce8
commit b6b52e62b2
2 changed files with 19 additions and 8 deletions

View File

@ -831,7 +831,7 @@ export class ElementInjector extends TreeNode<ElementInjector> {
directParent(): ElementInjector { return this._proto.distanceToParent < 2 ? this.parent : null; } directParent(): ElementInjector { return this._proto.distanceToParent < 2 ? this.parent : null; }
private _isComponentKey(key: Key) { private _isComponentKey(key: Key) {
return this._proto._firstBindingIsComponent && key.id === this._proto._keyId0; return this._proto._firstBindingIsComponent && isPresent(key) && key.id === this._proto._keyId0;
} }
private _isDynamicallyLoadedComponentKey(key: Key) { private _isDynamicallyLoadedComponentKey(key: Key) {
@ -1134,7 +1134,11 @@ export class ElementInjector extends TreeNode<ElementInjector> {
private _getByKey(key: Key, visibility: Visibility, optional: boolean, requestor: Key) { private _getByKey(key: Key, visibility: Visibility, optional: boolean, requestor: Key) {
var ei = this; var ei = this;
var currentVisibility = LIGHT_DOM; var currentVisibility = this._isComponentKey(requestor) ?
LIGHT_DOM_AND_SHADOW_DOM : // component can access both shadow dom
// and light dom dependencies
LIGHT_DOM;
var depth = visibility.depth; var depth = visibility.depth;
if (!visibility.shouldIncludeSelf()) { if (!visibility.shouldIncludeSelf()) {
@ -1144,9 +1148,7 @@ export class ElementInjector extends TreeNode<ElementInjector> {
ei = ei._parent; ei = ei._parent;
} else { } else {
ei = ei._host; ei = ei._host;
if (!visibility.crossComponentBoundaries) { currentVisibility = visibility.crossComponentBoundaries ? LIGHT_DOM : SHADOW_DOM;
currentVisibility = SHADOW_DOM;
}
} }
} }
@ -1159,15 +1161,14 @@ export class ElementInjector extends TreeNode<ElementInjector> {
depth -= ei._proto.distanceToParent; depth -= ei._proto.distanceToParent;
// we check only one mode with the SHADOW_DOM visibility
if (currentVisibility === SHADOW_DOM) break; if (currentVisibility === SHADOW_DOM) break;
if (isPresent(ei._parent)) { if (isPresent(ei._parent)) {
ei = ei._parent; ei = ei._parent;
} else { } else {
ei = ei._host; ei = ei._host;
if (!visibility.crossComponentBoundaries) { currentVisibility = visibility.crossComponentBoundaries ? LIGHT_DOM : SHADOW_DOM;
currentVisibility = SHADOW_DOM;
}
} }
} }

View File

@ -528,6 +528,16 @@ export function main() {
expect(inj.get('injectable2')).toEqual('injectable1-injectable2'); expect(inj.get('injectable2')).toEqual('injectable1-injectable2');
}); });
it("should instantiate components that depends on viewInjector dependencies", function () {
var inj = injector([
DirectiveBinding.createFromType(NeedsService,
new DummyDirective({viewInjector: [
bind('service').toValue('service')
]}))
], null, true);
expect(inj.get(NeedsService).service).toEqual('service');
});
it("should instantiate directives that depend on app services", function () { it("should instantiate directives that depend on app services", function () {
var appInjector = Injector.resolveAndCreate([ var appInjector = Injector.resolveAndCreate([
bind("service").toValue("service") bind("service").toValue("service")