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

Commit 95a7e3c

Browse files
committed
feature(directive_injector): add View to DirectiveInjector
Some directives require access to the current view. Add the view field to the DirectiveInjector class, so it can be injected.
1 parent 43b6f4f commit 95a7e3c

6 files changed

+30
-20
lines changed

lib/core_dom/directive_injector.dart

+12-7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class DirectiveInjector implements DirectiveBinder {
107107
final Animate _animate;
108108
final EventHandler _eventHandler;
109109
Scope scope; //TODO(misko): this should be final after we get rid of controller
110+
final View _view;
110111

111112
NgElement _ngElement;
112113
ElementProbe _elementProbe;
@@ -140,15 +141,18 @@ class DirectiveInjector implements DirectiveBinder {
140141

141142
static Binding _tempBinding = new Binding();
142143

143-
DirectiveInjector(this._parent, appInjector, this._node, this._nodeAttrs,
144-
this._eventHandler, this.scope, this._animate)
145-
: _appInjector = appInjector;
144+
DirectiveInjector(DirectiveInjector parent, appInjector, this._node, this._nodeAttrs,
145+
this._eventHandler, this.scope, this._animate, [View view])
146+
: _parent = parent,
147+
_appInjector = appInjector,
148+
_view = view == null && parent != null ? parent._view : view;
146149

147150
DirectiveInjector._default(this._parent, this._appInjector)
148151
: _node = null,
149152
_nodeAttrs = null,
150153
_eventHandler = null,
151154
scope = null,
155+
_view = null,
152156
_animate = null;
153157

154158
void bind(key, {dynamic toValue: DEFAULT_VALUE,
@@ -291,6 +295,7 @@ class DirectiveInjector implements DirectiveBinder {
291295
currentInjector = currentInjector._parent;
292296
}
293297
return null;
298+
case VIEW_KEY_ID: return _view;
294299
default: new NoProviderError(_KEYS[keyId]);
295300
}
296301
}
@@ -371,8 +376,8 @@ class TemplateDirectiveInjector extends DirectiveInjector {
371376

372377
TemplateDirectiveInjector(DirectiveInjector parent, Injector appInjector,
373378
Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
374-
Scope scope, Animate animate, this._viewFactory)
375-
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, animate);
379+
Scope scope, Animate animate, this._viewFactory, [View view])
380+
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, animate, view);
376381

377382

378383
Object _getById(int keyId) {
@@ -396,9 +401,9 @@ class ComponentDirectiveInjector extends DirectiveInjector {
396401

397402
ComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
398403
EventHandler eventHandler, Scope scope,
399-
this._templateLoader, this._shadowRoot, this._contentPort)
404+
this._templateLoader, this._shadowRoot, this._contentPort, [View view])
400405
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope,
401-
parent._animate);
406+
parent._animate, view);
402407

403408
Object _getById(int keyId) {
404409
switch(keyId) {

lib/core_dom/element_binder.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ class ElementBinder {
268268
eventHandler(parentInjector);
269269
if (this is TemplateElementBinder) {
270270
nodeInjector = new TemplateDirectiveInjector(parentInjector, _appInjector,
271-
node, nodeAttrs, parentEventHandler, scope, _animate, (this as TemplateElementBinder).templateViewFactory);
271+
node, nodeAttrs, parentEventHandler, scope, _animate, (this as TemplateElementBinder).templateViewFactory, view);
272272
} else {
273-
nodeInjector = new DirectiveInjector(parentInjector, _appInjector, node, nodeAttrs, parentEventHandler, scope, _animate);
273+
nodeInjector = new DirectiveInjector(parentInjector, _appInjector, node, nodeAttrs, parentEventHandler, scope, _animate, view);
274274
}
275275

276276
for(var i = 0; i < directiveRefs.length; i++) {

lib/core_dom/shadow_dom_component_factory.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ class BoundShadowDomComponentFactory implements BoundComponentFactory {
121121
}
122122

123123
List<Key> get callArgs => _CALL_ARGS;
124-
static final _CALL_ARGS = [DIRECTIVE_INJECTOR_KEY, SCOPE_KEY, NG_BASE_CSS_KEY,
124+
static final _CALL_ARGS = [DIRECTIVE_INJECTOR_KEY, SCOPE_KEY, VIEW_KEY, NG_BASE_CSS_KEY,
125125
EVENT_HANDLER_KEY];
126126
Function call(dom.Element element) {
127-
return (DirectiveInjector injector, Scope scope, NgBaseCss baseCss,
127+
return (DirectiveInjector injector, Scope scope, View view, NgBaseCss baseCss,
128128
EventHandler _) {
129129
var s = traceEnter(View_createComponent);
130130
try {
@@ -166,7 +166,7 @@ class BoundShadowDomComponentFactory implements BoundComponentFactory {
166166
var eventHandler = new ShadowRootEventHandler(
167167
shadowDom, injector.getByKey(EXPANDO_KEY), injector.getByKey(EXCEPTION_HANDLER_KEY));
168168
shadowInjector = new ComponentDirectiveInjector(injector, _injector, eventHandler, shadowScope,
169-
templateLoader, shadowDom, null);
169+
templateLoader, shadowDom, null, view);
170170
shadowInjector.bindByKey(_ref.typeKey, _ref.factory, _ref.paramKeys, _ref.annotation.visibility);
171171

172172
if (_componentFactory.config.elementProbeEnabled) {

lib/core_dom/transcluding_component_factory.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
9898
}
9999

100100
List<Key> get callArgs => _CALL_ARGS;
101-
static var _CALL_ARGS = [ DIRECTIVE_INJECTOR_KEY, SCOPE_KEY,
101+
static var _CALL_ARGS = [ DIRECTIVE_INJECTOR_KEY, SCOPE_KEY, VIEW_KEY,
102102
VIEW_CACHE_KEY, HTTP_KEY, TEMPLATE_CACHE_KEY,
103103
DIRECTIVE_MAP_KEY, NG_BASE_CSS_KEY, EVENT_HANDLER_KEY];
104104
Function call(dom.Node node) {
@@ -107,7 +107,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
107107
_component.cssUrls.isEmpty);
108108

109109
var element = node as dom.Element;
110-
return (DirectiveInjector injector, Scope scope,
110+
return (DirectiveInjector injector, Scope scope, View view,
111111
ViewCache viewCache, Http http, TemplateCache templateCache,
112112
DirectiveMap directives, NgBaseCss baseCss, EventHandler eventHandler) {
113113

@@ -145,7 +145,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
145145

146146
childInjector = new ComponentDirectiveInjector(injector, this._injector,
147147
eventHandler, shadowScope, templateLoader, new ShadowlessShadowRoot(element),
148-
contentPort);
148+
contentPort, view);
149149
childInjector.bindByKey(_ref.typeKey, _ref.factory, _ref.paramKeys, _ref.annotation.visibility);
150150

151151
if (childInjectorCompleter != null) {

test/core_dom/directive_injector_spec.dart

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void main() {
1515
describe('base', () {
1616
DirectiveInjector injector;
1717
Scope scope;
18+
View view;
1819
Animate animate;
1920

2021
addDirective(Type type, [Visibility visibility]) {
@@ -30,13 +31,16 @@ void main() {
3031
beforeEach((Scope _scope, Animate _animate) {
3132
scope = _scope;
3233
animate = _animate;
33-
injector = new DirectiveInjector(null, appInjector, div, new NodeAttrs(div), eventHandler, scope, animate);
34+
view = new View([], scope);
35+
injector = new DirectiveInjector(null, appInjector, div, new NodeAttrs(div), eventHandler,
36+
scope, animate, view);
3437
});
3538

3639
it('should return basic types', () {
3740
expect(injector.scope).toBe(scope);
3841
expect(injector.get(Injector)).toBe(appInjector);
3942
expect(injector.get(Scope)).toBe(scope);
43+
expect((injector.get(View))).toBe(view);
4044
expect(injector.get(Node)).toBe(div);
4145
expect(injector.get(Element)).toBe(div);
4246
expect((injector.get(NodeAttrs) as NodeAttrs).element).toBe(div);
@@ -48,7 +52,8 @@ void main() {
4852
it('should support get from parent methods', () {
4953
var newDiv = new DivElement();
5054
var childInjector = new DirectiveInjector(
51-
injector, appInjector, newDiv, new NodeAttrs(newDiv), eventHandler, scope, animate);
55+
injector, appInjector, newDiv, new NodeAttrs(newDiv), eventHandler,
56+
scope, animate, view);
5257

5358
expect(childInjector.get(Node)).toBe(newDiv);
5459
expect(childInjector.getFromParent(Node)).toBe(div);
@@ -79,8 +84,8 @@ void main() {
7984
DirectiveInjector leafInjector;
8085

8186
beforeEach(() {
82-
childInjector = new DirectiveInjector(injector, appInjector, span, null, null, null, null);
83-
leafInjector = new DirectiveInjector(childInjector, appInjector, span, null, null, null, null);
87+
childInjector = new DirectiveInjector(injector, appInjector, span, null, null, null, null, null);
88+
leafInjector = new DirectiveInjector(childInjector, appInjector, span, null, null, null, null, null);
8489
});
8590

8691
it('should not allow reseting visibility', () {

test/directive/ng_model_spec.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void main() {
4747

4848
beforeEach((TestBed tb) {
4949
_ = tb;
50-
dirInjector = new DirectiveInjector(null, _.injector, null, null, null, null, null);
50+
dirInjector = new DirectiveInjector(null, _.injector, null, null, null, null, null, null);
5151
});
5252

5353
describe('type="text" like', () {

0 commit comments

Comments
 (0)