feat(element_injector): add distance to propertly implement @parent

This commit is contained in:
vsavkin
2015-01-10 12:16:08 -08:00
parent bed4b52a63
commit 3c692a1b85
6 changed files with 104 additions and 39 deletions

View File

@ -103,7 +103,7 @@ export function main() {
parent.instantiateDirectives(inj, null, parentPreBuildObjects);
var protoChild = new ProtoElementInjector(protoParent, 1, childBindings);
var protoChild = new ProtoElementInjector(protoParent, 1, childBindings, false, 1);
var child = protoChild.instantiate(parent, null);
child.instantiateDirectives(inj, null, defaultPreBuiltObjects);
@ -120,7 +120,7 @@ export function main() {
var host = protoParent.instantiate(null, null);
host.instantiateDirectives(inj, shadowInj, hostPreBuildObjects);
var protoChild = new ProtoElementInjector(protoParent, 0, shadowBindings, false);
var protoChild = new ProtoElementInjector(protoParent, 0, shadowBindings, false, 1);
var shadow = protoChild.instantiate(null, host);
shadow.instantiateDirectives(shadowInj, null, null);
@ -144,6 +144,30 @@ export function main() {
[c2, 'child2']
])).toEqual(["parent", ["child1", "child2"]]);
});
describe("direct parent", () => {
it("should return parent injector when distance is 1", () => {
var distance = 1;
var protoParent = new ProtoElementInjector(null, 0, []);
var protoChild = new ProtoElementInjector(protoParent, 1, [], false, distance);
var p = protoParent.instantiate(null, null);
var c = protoChild.instantiate(p, null);
expect(c.directParent()).toEqual(p);
});
it("should return null otherwise", () => {
var distance = 2;
var protoParent = new ProtoElementInjector(null, 0, []);
var protoChild = new ProtoElementInjector(protoParent, 1, [], false, distance);
var p = protoParent.instantiate(null, null);
var c = protoChild.instantiate(p, null);
expect(c.directParent()).toEqual(null);
});
});
});
describe("hasBindings", function () {

View File

@ -128,7 +128,6 @@ export function main() {
expect(DOM.getText(view.nodes[0])).toEqual('Before LightDOM After');
done();
});
});
});
}

View File

@ -73,32 +73,57 @@ export function main() {
expect(creationArgs['index']).toBe(protoView.elementBinders.length);
});
it('should inherit the ProtoElementInjector down to children without directives', () => {
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(createElement('<div directives><span></span></div>'));
expect(results[1].inheritedProtoElementInjector).toBe(results[0].inheritedProtoElementInjector);
describe("inheritedProtoElementInjector", () => {
it('should inherit the ProtoElementInjector down to children without directives', () => {
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(createElement('<div directives><span></span></div>'));
expect(results[1].inheritedProtoElementInjector).toBe(results[0].inheritedProtoElementInjector);
});
it('should use the ProtoElementInjector of the parent element as parent', () => {
var el = createElement('<div directives><span><a directives></a></span></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[2].inheritedProtoElementInjector.parent).toBe(
results[0].inheritedProtoElementInjector);
});
it('should use a null parent for viewRoots', () => {
var el = createElement('<div directives><span viewroot directives></span></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[1].inheritedProtoElementInjector.parent).toBe(null);
});
it('should use a null parent if there is an intermediate viewRoot', () => {
var el = createElement('<div directives><span viewroot><a directives></a></span></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[2].inheritedProtoElementInjector.parent).toBe(null);
});
});
it('should use the ProtoElementInjector of the parent element as parent', () => {
var el = createElement('<div directives><span><a directives></a></span></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[2].inheritedProtoElementInjector.parent).toBe(
results[0].inheritedProtoElementInjector);
});
describe("distanceToParentInjector", () => {
it("should be 0 for root elements", () => {
var el = createElement('<div directives></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[0].inheritedProtoElementInjector.distanceToParent).toBe(0);
});
it('should use a null parent for viewRoots', () => {
var el = createElement('<div directives><span viewroot directives></span></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[1].inheritedProtoElementInjector.parent).toBe(null);
});
it("should be 1 when a parent element has an injector", () => {
var el = createElement('<div directives><span directives></span></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[1].inheritedProtoElementInjector.distanceToParent).toBe(1);
});
it('should use a null parent if there is an intermediate viewRoot', () => {
var el = createElement('<div directives><span viewroot><a directives></a></span></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[2].inheritedProtoElementInjector.parent).toBe(null);
it("should add 1 for every element that does not have an injector", () => {
var el = createElement('<div directives><a><b><span directives></span></b></a></div>');
var directives = [SomeDecoratorDirective];
var results = createPipeline(directives).process(el);
expect(results[3].inheritedProtoElementInjector.distanceToParent).toBe(3);
});
});
});
}
@ -117,8 +142,8 @@ class TestableProtoElementInjectorBuilder extends ProtoElementInjectorBuilder {
}
return null;
}
internalCreateProtoElementInjector(parent, index, bindings, firstBindingIsComponent) {
var result = new ProtoElementInjector(parent, index, bindings, firstBindingIsComponent);
internalCreateProtoElementInjector(parent, index, bindings, firstBindingIsComponent, distance) {
var result = new ProtoElementInjector(parent, index, bindings, firstBindingIsComponent, distance);
ListWrapper.push(this.debugObjects, result);
ListWrapper.push(this.debugObjects, {'parent': parent, 'index': index, 'bindings': bindings, 'firstBindingIsComponent': firstBindingIsComponent});
return result;