feat(di): added optional self parameter to Parent, Ancestor, and Unbounded

This commit is contained in:
vsavkin
2015-05-27 16:43:22 -07:00
parent ebe1e73b1a
commit 34cfc9f474
3 changed files with 27 additions and 9 deletions

View File

@ -1,11 +1,14 @@
import {CONST, CONST_EXPR} from 'angular2/src/facade/lang'; import {CONST, CONST_EXPR, isBlank} from 'angular2/src/facade/lang';
import {DependencyAnnotation} from 'angular2/src/di/annotations_impl'; import {DependencyAnnotation} from 'angular2/src/di/annotations_impl';
@CONST() @CONST()
export class Visibility extends DependencyAnnotation { export class Visibility extends DependencyAnnotation {
constructor(public depth: number, public crossComponentBoundaries: boolean) { super(); } constructor(public depth: number, public crossComponentBoundaries: boolean,
public _includeSelf: boolean) {
super();
}
shouldIncludeSelf(): boolean { return this.depth === 0; } get includeSelf(): boolean { return isBlank(this._includeSelf) ? false : this._includeSelf; }
} }
/** /**
@ -47,7 +50,7 @@ export class Visibility extends DependencyAnnotation {
*/ */
@CONST() @CONST()
export class Self extends Visibility { export class Self extends Visibility {
constructor() { super(0, false); } constructor() { super(0, false, true); }
} }
// make constants after switching to ts2dart // make constants after switching to ts2dart
@ -98,7 +101,7 @@ export var self = new Self();
*/ */
@CONST() @CONST()
export class Parent extends Visibility { export class Parent extends Visibility {
constructor() { super(1, false); } constructor({self}: {self?: boolean} = {}) { super(1, false, self); }
} }
/** /**
@ -159,7 +162,7 @@ export class Parent extends Visibility {
*/ */
@CONST() @CONST()
export class Ancestor extends Visibility { export class Ancestor extends Visibility {
constructor() { super(999999, false); } constructor({self}: {self?: boolean} = {}) { super(999999, false, self); }
} }
/** /**
@ -198,5 +201,5 @@ export class Ancestor extends Visibility {
*/ */
@CONST() @CONST()
export class Unbounded extends Visibility { export class Unbounded extends Visibility {
constructor() { super(999999, true); } constructor({self}: {self?: boolean} = {}) { super(999999, true, self); }
} }

View File

@ -1082,7 +1082,7 @@ export class ElementInjector extends TreeNode<ElementInjector> {
var depth = visibility.depth; var depth = visibility.depth;
if (!visibility.shouldIncludeSelf()) { if (!visibility.includeSelf) {
depth -= ei._proto.distanceToParent; depth -= ei._proto.distanceToParent;
if (isPresent(ei._parent)) { if (isPresent(ei._parent)) {

View File

@ -103,6 +103,12 @@ class NeedsDirectiveFromParent {
constructor(@Parent() dependency: SimpleDirective) { this.dependency = dependency; } constructor(@Parent() dependency: SimpleDirective) { this.dependency = dependency; }
} }
@Injectable()
class NeedsDirectiveFromParentOrSelf {
dependency: SimpleDirective;
constructor(@Parent({self:true}) dependency: SimpleDirective) { this.dependency = dependency; }
}
@Injectable() @Injectable()
class NeedsDirectiveFromAncestor { class NeedsDirectiveFromAncestor {
dependency: SimpleDirective; dependency: SimpleDirective;
@ -640,12 +646,21 @@ export function main() {
expect(d.dependency).toBeAnInstanceOf(SimpleDirective); expect(d.dependency).toBeAnInstanceOf(SimpleDirective);
}); });
it("should not return parent's directives on self", () => { it("should not return parent's directives on self by default", () => {
expect(() => { expect(() => {
injector(ListWrapper.concat([SimpleDirective, NeedsDirectiveFromParent], extraBindings)); injector(ListWrapper.concat([SimpleDirective, NeedsDirectiveFromParent], extraBindings));
}).toThrowError(containsRegexp(`No provider for ${stringify(SimpleDirective) }`)); }).toThrowError(containsRegexp(`No provider for ${stringify(SimpleDirective) }`));
}); });
it("should return parent's directives on self when explicitly specified", () => {
var inj = injector(ListWrapper.concat([SimpleDirective, NeedsDirectiveFromParentOrSelf], extraBindings));
var d = inj.get(NeedsDirectiveFromParentOrSelf);
expect(d).toBeAnInstanceOf(NeedsDirectiveFromParentOrSelf);
expect(d.dependency).toBeAnInstanceOf(SimpleDirective);
});
it("should get directives from ancestor", () => { it("should get directives from ancestor", () => {
var child = parentChildInjectors(ListWrapper.concat([SimpleDirective], extraBindings), var child = parentChildInjectors(ListWrapper.concat([SimpleDirective], extraBindings),
[NeedsDirectiveFromAncestor]); [NeedsDirectiveFromAncestor]);