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

Commit

Permalink
feat(directive-injector): component directive injector injects parent
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rkirov committed Aug 25, 2014
1 parent 4b67ea4 commit 3af9434
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
14 changes: 12 additions & 2 deletions lib/core_dom/directive_injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var _TAG_GET = new UserTag('DirectiveInjector.get()');
var _TAG_INSTANTIATE = new UserTag('DirectiveInjector.instantiate()');

final DIRECTIVE_INJECTOR_KEY = new Key(DirectiveInjector);
final COMPONENT_DIRECTIVE_INJECTOR_KEY = new Key(ComponentDirectiveInjector);
final CONTENT_PORT_KEY = new Key(ContentPort);
final TEMPLATE_LOADER_KEY = new Key(TemplateLoader);
final SHADOW_ROOT_KEY = new Key(ShadowRoot);
Expand Down Expand Up @@ -48,7 +49,8 @@ const int TEMPLATE_LOADER_KEY_ID = 14;
const int SHADOW_ROOT_KEY_ID = 15;
const int CONTENT_PORT_KEY_ID = 16;
const int EVENT_HANDLER_KEY_ID = 17;
const int KEEP_ME_LAST = 18;
const int COMPONENT_DIRECTIVE_INJECTOR_KEY_ID = 18;
const int KEEP_ME_LAST = 19;

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

Expand All @@ -74,6 +76,7 @@ class DirectiveInjector implements DirectiveBinder {
CONTENT_PORT_KEY.uid = CONTENT_PORT_KEY_ID;
EVENT_HANDLER_KEY.uid = EVENT_HANDLER_KEY_ID;
ANIMATE_KEY.uid = ANIMATE_KEY_ID;
COMPONENT_DIRECTIVE_INJECTOR_KEY.uid = COMPONENT_DIRECTIVE_INJECTOR_KEY_ID;
for(var i = 1; i < KEEP_ME_LAST; i++) {
if (_KEYS[i].uid != i) throw 'MISSORDERED KEYS ARRAY: ${_KEYS} at $i';
}
Expand All @@ -97,6 +100,7 @@ class DirectiveInjector implements DirectiveBinder {
, SHADOW_ROOT_KEY
, CONTENT_PORT_KEY
, EVENT_HANDLER_KEY
, COMPONENT_DIRECTIVE_INJECTOR_KEY
, KEEP_ME_LAST
];

Expand Down Expand Up @@ -398,12 +402,18 @@ class ComponentDirectiveInjector extends DirectiveInjector {
EventHandler eventHandler, Scope scope,
this._templateLoader, this._shadowRoot, this._contentPort)
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope,
parent._animate);
parent._animate) {
// A single component creates a ComponentDirectiveInjector and its DirectiveInjector parent,
// so parent should never be null.
assert(parent != null);
}

Object _getById(int keyId) {
switch(keyId) {
case TEMPLATE_LOADER_KEY_ID: return _templateLoader;
case SHADOW_ROOT_KEY_ID: return _shadowRoot;
case DIRECTIVE_INJECTOR_KEY_ID: return _parent;
case COMPONENT_DIRECTIVE_INJECTOR_KEY_ID: return this;
default: return super._getById(keyId);
}
}
Expand Down
22 changes: 21 additions & 1 deletion test/core_dom/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ void main() {
..bind(ScopeAwareComponent)
..bind(Parent, toValue: null)
..bind(Child)
..bind(ChildTemplateComponent);
..bind(ChildTemplateComponent)
..bind(InjectorDependentComponent);
});

beforeEach((TestBed tb) => _ = tb);
Expand Down Expand Up @@ -676,6 +677,13 @@ void main() {
expect(logger.length).toEqual(2);
}));

it('should inject the correct Injectors - Directive and ComponentDirective', async(() {
_.compile('<cmp-inj></cmp-inj>');
_.rootScope.apply();
microLeap();
// assertions are in the component constructor.
}));

describe('lifecycle', () {
beforeEachModule((Module module) {
var httpBackend = new MockHttpBackend();
Expand Down Expand Up @@ -1553,3 +1561,15 @@ class OnceInside {
set v(x) { log(x); ot = "($x)"; }
OnceInside(Logger this.log) { log('!'); }
}

@Component(
selector: 'cmp-inj')
class InjectorDependentComponent {
DirectiveInjector i;
ComponentDirectiveInjector cdi;
InjectorDependentComponent(this.i, this.cdi) {
expect(i).toBeAnInstanceOf(DirectiveInjector);
expect(cdi).toBeAnInstanceOf(ComponentDirectiveInjector);
expect(cdi.parent).toBe(i);
}
}

0 comments on commit 3af9434

Please sign in to comment.