feat(di): added optional self parameter to Parent, Ancestor, and Unbounded
This commit is contained in:
parent
ebe1e73b1a
commit
34cfc9f474
|
@ -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';
|
||||
|
||||
@CONST()
|
||||
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()
|
||||
export class Self extends Visibility {
|
||||
constructor() { super(0, false); }
|
||||
constructor() { super(0, false, true); }
|
||||
}
|
||||
|
||||
// make constants after switching to ts2dart
|
||||
|
@ -98,7 +101,7 @@ export var self = new Self();
|
|||
*/
|
||||
@CONST()
|
||||
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()
|
||||
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()
|
||||
export class Unbounded extends Visibility {
|
||||
constructor() { super(999999, true); }
|
||||
constructor({self}: {self?: boolean} = {}) { super(999999, true, self); }
|
||||
}
|
||||
|
|
|
@ -1082,7 +1082,7 @@ export class ElementInjector extends TreeNode<ElementInjector> {
|
|||
|
||||
var depth = visibility.depth;
|
||||
|
||||
if (!visibility.shouldIncludeSelf()) {
|
||||
if (!visibility.includeSelf) {
|
||||
depth -= ei._proto.distanceToParent;
|
||||
|
||||
if (isPresent(ei._parent)) {
|
||||
|
|
|
@ -103,6 +103,12 @@ class NeedsDirectiveFromParent {
|
|||
constructor(@Parent() dependency: SimpleDirective) { this.dependency = dependency; }
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
class NeedsDirectiveFromParentOrSelf {
|
||||
dependency: SimpleDirective;
|
||||
constructor(@Parent({self:true}) dependency: SimpleDirective) { this.dependency = dependency; }
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
class NeedsDirectiveFromAncestor {
|
||||
dependency: SimpleDirective;
|
||||
|
@ -640,12 +646,21 @@ export function main() {
|
|||
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(() => {
|
||||
injector(ListWrapper.concat([SimpleDirective, NeedsDirectiveFromParent], extraBindings));
|
||||
}).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", () => {
|
||||
var child = parentChildInjectors(ListWrapper.concat([SimpleDirective], extraBindings),
|
||||
[NeedsDirectiveFromAncestor]);
|
||||
|
|
Loading…
Reference in New Issue