Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 87d72ef

Browse files
committed
feat(directive-injector): component directive injector injects parent
BREAKING_CHANGE: When asked for DirectiveInjector, ComponentDirectiveInjector injects parent. When asked for ComponentDirectiveInjector it injects self. Before: MyComponent(DirectiveInjector di) { } After: MyComponent(ComponentDirectiveInjector cdi, DirectiveInjector di) { } where before di = after cdi, before di.parent = after di. Closes #1351
1 parent 968727f commit 87d72ef

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/core_dom/directive_injector.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var _TAG_GET = new UserTag('DirectiveInjector.get()');
2020
var _TAG_INSTANTIATE = new UserTag('DirectiveInjector.instantiate()');
2121

2222
final DIRECTIVE_INJECTOR_KEY = new Key(DirectiveInjector);
23+
final COMPONENT_DIRECTIVE_INJECTOR_KEY = new Key(ComponentDirectiveInjector);
2324
final CONTENT_PORT_KEY = new Key(ContentPort);
2425
final TEMPLATE_LOADER_KEY = new Key(TemplateLoader);
2526
final SHADOW_ROOT_KEY = new Key(ShadowRoot);
@@ -48,7 +49,8 @@ const int TEMPLATE_LOADER_KEY_ID = 14;
4849
const int SHADOW_ROOT_KEY_ID = 15;
4950
const int CONTENT_PORT_KEY_ID = 16;
5051
const int EVENT_HANDLER_KEY_ID = 17;
51-
const int KEEP_ME_LAST = 18;
52+
const int COMPONENT_DIRECTIVE_INJECTOR_KEY_ID = 18;
53+
const int KEEP_ME_LAST = 19;
5254

5355
EventHandler eventHandler(DirectiveInjector di) => di._eventHandler;
5456

@@ -74,6 +76,7 @@ class DirectiveInjector implements DirectiveBinder {
7476
CONTENT_PORT_KEY.uid = CONTENT_PORT_KEY_ID;
7577
EVENT_HANDLER_KEY.uid = EVENT_HANDLER_KEY_ID;
7678
ANIMATE_KEY.uid = ANIMATE_KEY_ID;
79+
COMPONENT_DIRECTIVE_INJECTOR_KEY.uid = COMPONENT_DIRECTIVE_INJECTOR_KEY_ID;
7780
for(var i = 1; i < KEEP_ME_LAST; i++) {
7881
if (_KEYS[i].uid != i) throw 'MISSORDERED KEYS ARRAY: ${_KEYS} at $i';
7982
}
@@ -97,6 +100,7 @@ class DirectiveInjector implements DirectiveBinder {
97100
, SHADOW_ROOT_KEY
98101
, CONTENT_PORT_KEY
99102
, EVENT_HANDLER_KEY
103+
, COMPONENT_DIRECTIVE_INJECTOR_KEY
100104
, KEEP_ME_LAST
101105
];
102106

@@ -398,12 +402,18 @@ class ComponentDirectiveInjector extends DirectiveInjector {
398402
EventHandler eventHandler, Scope scope,
399403
this._templateLoader, this._shadowRoot, this._contentPort)
400404
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope,
401-
parent._animate);
405+
parent._animate) {
406+
// A single component creates a ComponentDirectiveInjector and its DirectiveInjector parent,
407+
// so parent should never be null.
408+
assert(parent != null);
409+
}
402410

403411
Object _getById(int keyId) {
404412
switch(keyId) {
405413
case TEMPLATE_LOADER_KEY_ID: return _templateLoader;
406414
case SHADOW_ROOT_KEY_ID: return _shadowRoot;
415+
case DIRECTIVE_INJECTOR_KEY_ID: return _parent;
416+
case COMPONENT_DIRECTIVE_INJECTOR_KEY_ID: return this;
407417
default: return super._getById(keyId);
408418
}
409419
}

test/core_dom/compiler_spec.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ void main() {
8282
..bind(ScopeAwareComponent)
8383
..bind(Parent, toValue: null)
8484
..bind(Child)
85-
..bind(ChildTemplateComponent);
85+
..bind(ChildTemplateComponent)
86+
..bind(InjectorDependentComponent);
8687
});
8788

8889
beforeEach((TestBed tb) => _ = tb);
@@ -676,6 +677,13 @@ void main() {
676677
expect(logger.length).toEqual(2);
677678
}));
678679

680+
it('should inject the correct Injectors - Directive and ComponentDirective', async(() {
681+
_.compile('<cmp-inj></cmp-inj>');
682+
_.rootScope.apply();
683+
microLeap();
684+
// assertions are in the component constructor.
685+
}));
686+
679687
describe('lifecycle', () {
680688
beforeEachModule((Module module) {
681689
var httpBackend = new MockHttpBackend();
@@ -1553,3 +1561,15 @@ class OnceInside {
15531561
set v(x) { log(x); ot = "($x)"; }
15541562
OnceInside(Logger this.log) { log('!'); }
15551563
}
1564+
1565+
@Component(
1566+
selector: 'cmp-inj')
1567+
class InjectorDependentComponent {
1568+
DirectiveInjector i;
1569+
ComponentDirectiveInjector cdi;
1570+
InjectorDependentComponent(this.i, this.cdi) {
1571+
expect(i).toBeAnInstanceOf(DirectiveInjector);
1572+
expect(cdi).toBeAnInstanceOf(ComponentDirectiveInjector);
1573+
expect(cdi.parent).toBe(i);
1574+
}
1575+
}

0 commit comments

Comments
 (0)