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

Commit aa1fcfd

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 30cea03 commit aa1fcfd

6 files changed

+26
-18
lines changed

lib/core_dom/directive_injector.dart

+8-5
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;
@@ -141,14 +142,15 @@ class DirectiveInjector implements DirectiveBinder {
141142
static Binding _tempBinding = new Binding();
142143

143144
DirectiveInjector(this._parent, appInjector, this._node, this._nodeAttrs,
144-
this._eventHandler, this.scope, this._animate)
145+
this._eventHandler, this.scope, this._view, this._animate)
145146
: _appInjector = appInjector;
146147

147148
DirectiveInjector._default(this._parent, this._appInjector)
148149
: _node = null,
149150
_nodeAttrs = null,
150151
_eventHandler = null,
151152
scope = null,
153+
_view = null,
152154
_animate = null;
153155

154156
void bind(key, {dynamic toValue: DEFAULT_VALUE,
@@ -291,6 +293,7 @@ class DirectiveInjector implements DirectiveBinder {
291293
currentInjector = currentInjector._parent;
292294
}
293295
return null;
296+
case VIEW_KEY_ID: return _view;
294297
default: new NoProviderError(_KEYS[keyId]);
295298
}
296299
}
@@ -371,8 +374,8 @@ class TemplateDirectiveInjector extends DirectiveInjector {
371374

372375
TemplateDirectiveInjector(DirectiveInjector parent, Injector appInjector,
373376
Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
374-
Scope scope, Animate animate, this._viewFactory)
375-
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, animate);
377+
Scope scope, View view, Animate animate, this._viewFactory)
378+
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, view, animate);
376379

377380

378381
Object _getById(int keyId) {
@@ -395,9 +398,9 @@ class ComponentDirectiveInjector extends DirectiveInjector {
395398
final ContentPort _contentPort;
396399

397400
ComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
398-
EventHandler eventHandler, Scope scope,
401+
EventHandler eventHandler, Scope scope, View view,
399402
this._templateLoader, this._shadowRoot, this._contentPort)
400-
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope,
403+
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope, view,
401404
parent._animate);
402405

403406
Object _getById(int 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, view, _animate, (this as TemplateElementBinder).templateViewFactory);
272272
} else {
273-
nodeInjector = new DirectiveInjector(parentInjector, _appInjector, node, nodeAttrs, parentEventHandler, scope, _animate);
273+
nodeInjector = new DirectiveInjector(parentInjector, _appInjector, node, nodeAttrs, parentEventHandler, scope, view, _animate);
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+
view, templateLoader, shadowDom, null);
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

@@ -144,7 +144,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
144144
Scope shadowScope = scope.createChild(new HashMap());
145145

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

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, view, animate);
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, view, animate);
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)