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

Commit 42c31ec

Browse files
vsavkinjbdeboer
authored andcommitted
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 4bcf3ec commit 42c31ec

6 files changed

+30
-20
lines changed

lib/core_dom/directive_injector.dart

+12-7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class DirectiveInjector implements DirectiveBinder {
111111
final Animate _animate;
112112
final EventHandler _eventHandler;
113113
Scope scope; //TODO(misko): this should be final after we get rid of controller
114+
final View _view;
114115

115116
NgElement _ngElement;
116117
ElementProbe _elementProbe;
@@ -144,15 +145,18 @@ class DirectiveInjector implements DirectiveBinder {
144145

145146
static Binding _tempBinding = new Binding();
146147

147-
DirectiveInjector(this._parent, appInjector, this._node, this._nodeAttrs,
148-
this._eventHandler, this.scope, this._animate)
149-
: _appInjector = appInjector;
148+
DirectiveInjector(DirectiveInjector parent, appInjector, this._node, this._nodeAttrs,
149+
this._eventHandler, this.scope, this._animate, [View view])
150+
: _parent = parent,
151+
_appInjector = appInjector,
152+
_view = view == null && parent != null ? parent._view : view;
150153

151154
DirectiveInjector._default(this._parent, this._appInjector)
152155
: _node = null,
153156
_nodeAttrs = null,
154157
_eventHandler = null,
155158
scope = null,
159+
_view = null,
156160
_animate = null;
157161

158162
void bind(key, {dynamic toValue: DEFAULT_VALUE,
@@ -295,6 +299,7 @@ class DirectiveInjector implements DirectiveBinder {
295299
currentInjector = currentInjector._parent;
296300
}
297301
return null;
302+
case VIEW_KEY_ID: return _view;
298303
default: new NoProviderError(_KEYS[keyId]);
299304
}
300305
}
@@ -375,8 +380,8 @@ class TemplateDirectiveInjector extends DirectiveInjector {
375380

376381
TemplateDirectiveInjector(DirectiveInjector parent, Injector appInjector,
377382
Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
378-
Scope scope, Animate animate, this._viewFactory)
379-
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, animate);
383+
Scope scope, Animate animate, this._viewFactory, [View view])
384+
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, animate, view);
380385

381386

382387
Object _getById(int keyId) {
@@ -400,9 +405,9 @@ class ComponentDirectiveInjector extends DirectiveInjector {
400405

401406
ComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
402407
EventHandler eventHandler, Scope scope,
403-
this._templateLoader, this._shadowRoot, this._contentPort)
408+
this._templateLoader, this._shadowRoot, this._contentPort, [View view])
404409
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope,
405-
parent._animate) {
410+
parent._animate, view) {
406411
// A single component creates a ComponentDirectiveInjector and its DirectiveInjector parent,
407412
// so parent should never be null.
408413
assert(parent != null);

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)