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

Commit

Permalink
feature(directive_injector): add View to DirectiveInjector
Browse files Browse the repository at this point in the history
Some directives require access to the current view. Add the view field to the DirectiveInjector class, so it can be injected.
  • Loading branch information
vsavkin committed Aug 24, 2014
1 parent 30cea03 commit aa1fcfd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
13 changes: 8 additions & 5 deletions lib/core_dom/directive_injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class DirectiveInjector implements DirectiveBinder {
final Animate _animate;
final EventHandler _eventHandler;
Scope scope; //TODO(misko): this should be final after we get rid of controller
final View _view;

NgElement _ngElement;
ElementProbe _elementProbe;
Expand Down Expand Up @@ -141,14 +142,15 @@ class DirectiveInjector implements DirectiveBinder {
static Binding _tempBinding = new Binding();

DirectiveInjector(this._parent, appInjector, this._node, this._nodeAttrs,
this._eventHandler, this.scope, this._animate)
this._eventHandler, this.scope, this._view, this._animate)
: _appInjector = appInjector;

DirectiveInjector._default(this._parent, this._appInjector)
: _node = null,
_nodeAttrs = null,
_eventHandler = null,
scope = null,
_view = null,
_animate = null;

void bind(key, {dynamic toValue: DEFAULT_VALUE,
Expand Down Expand Up @@ -291,6 +293,7 @@ class DirectiveInjector implements DirectiveBinder {
currentInjector = currentInjector._parent;
}
return null;
case VIEW_KEY_ID: return _view;
default: new NoProviderError(_KEYS[keyId]);
}
}
Expand Down Expand Up @@ -371,8 +374,8 @@ class TemplateDirectiveInjector extends DirectiveInjector {

TemplateDirectiveInjector(DirectiveInjector parent, Injector appInjector,
Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
Scope scope, Animate animate, this._viewFactory)
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, animate);
Scope scope, View view, Animate animate, this._viewFactory)
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, view, animate);


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

ComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
EventHandler eventHandler, Scope scope,
EventHandler eventHandler, Scope scope, View view,
this._templateLoader, this._shadowRoot, this._contentPort)
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope,
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope, view,
parent._animate);

Object _getById(int keyId) {
Expand Down
4 changes: 2 additions & 2 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ class ElementBinder {
eventHandler(parentInjector);
if (this is TemplateElementBinder) {
nodeInjector = new TemplateDirectiveInjector(parentInjector, _appInjector,
node, nodeAttrs, parentEventHandler, scope, _animate, (this as TemplateElementBinder).templateViewFactory);
node, nodeAttrs, parentEventHandler, scope, view, _animate, (this as TemplateElementBinder).templateViewFactory);
} else {
nodeInjector = new DirectiveInjector(parentInjector, _appInjector, node, nodeAttrs, parentEventHandler, scope, _animate);
nodeInjector = new DirectiveInjector(parentInjector, _appInjector, node, nodeAttrs, parentEventHandler, scope, view, _animate);
}

for(var i = 0; i < directiveRefs.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions lib/core_dom/shadow_dom_component_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ class BoundShadowDomComponentFactory implements BoundComponentFactory {
}

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

if (_componentFactory.config.elementProbeEnabled) {
Expand Down
6 changes: 3 additions & 3 deletions lib/core_dom/transcluding_component_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
}

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

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

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

childInjector = new ComponentDirectiveInjector(injector, this._injector,
eventHandler, shadowScope, templateLoader, new ShadowlessShadowRoot(element),
eventHandler, shadowScope, view, templateLoader, new ShadowlessShadowRoot(element),
contentPort);
childInjector.bindByKey(_ref.typeKey, _ref.factory, _ref.paramKeys, _ref.annotation.visibility);

Expand Down
13 changes: 9 additions & 4 deletions test/core_dom/directive_injector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void main() {
describe('base', () {
DirectiveInjector injector;
Scope scope;
View view;
Animate animate;

addDirective(Type type, [Visibility visibility]) {
Expand All @@ -30,13 +31,16 @@ void main() {
beforeEach((Scope _scope, Animate _animate) {
scope = _scope;
animate = _animate;
injector = new DirectiveInjector(null, appInjector, div, new NodeAttrs(div), eventHandler, scope, animate);
view = new View([], scope);
injector = new DirectiveInjector(null, appInjector, div, new NodeAttrs(div), eventHandler,
scope, view, animate);
});

it('should return basic types', () {
expect(injector.scope).toBe(scope);
expect(injector.get(Injector)).toBe(appInjector);
expect(injector.get(Scope)).toBe(scope);
expect((injector.get(View))).toBe(view);
expect(injector.get(Node)).toBe(div);
expect(injector.get(Element)).toBe(div);
expect((injector.get(NodeAttrs) as NodeAttrs).element).toBe(div);
Expand All @@ -48,7 +52,8 @@ void main() {
it('should support get from parent methods', () {
var newDiv = new DivElement();
var childInjector = new DirectiveInjector(
injector, appInjector, newDiv, new NodeAttrs(newDiv), eventHandler, scope, animate);
injector, appInjector, newDiv, new NodeAttrs(newDiv), eventHandler,
scope, view, animate);

expect(childInjector.get(Node)).toBe(newDiv);
expect(childInjector.getFromParent(Node)).toBe(div);
Expand Down Expand Up @@ -79,8 +84,8 @@ void main() {
DirectiveInjector leafInjector;

beforeEach(() {
childInjector = new DirectiveInjector(injector, appInjector, span, null, null, null, null);
leafInjector = new DirectiveInjector(childInjector, appInjector, span, null, null, null, null);
childInjector = new DirectiveInjector(injector, appInjector, span, null, null, null, null, null);
leafInjector = new DirectiveInjector(childInjector, appInjector, span, null, null, null, null, null);
});

it('should not allow reseting visibility', () {
Expand Down
2 changes: 1 addition & 1 deletion test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void main() {

beforeEach((TestBed tb) {
_ = tb;
dirInjector = new DirectiveInjector(null, _.injector, null, null, null, null, null);
dirInjector = new DirectiveInjector(null, _.injector, null, null, null, null, null, null);
});

describe('type="text" like', () {
Expand Down

0 comments on commit aa1fcfd

Please sign in to comment.