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): introduce getFromParent[byKey] methods on D…
Browse files Browse the repository at this point in the history
…irectiveInjector

First step towards deprecating injector.parent access.

The reason for the deprecation is that performing injector.parent.get
without keeping track of current injector.appInjector is usually wrong
(since the fallback would be injector.parent.appInjector).
  • Loading branch information
rkirov committed Aug 9, 2014
1 parent 2a11b3a commit 3b7b0d6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
36 changes: 20 additions & 16 deletions lib/core_dom/directive_injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class DirectiveInjector implements DirectiveBinder {
, KEEP_ME_LAST
];

final DirectiveInjector parent;
final DirectiveInjector _parent;
final Injector appInjector;
final Node _node;
final NodeAttrs _nodeAttrs;
Expand All @@ -122,6 +122,9 @@ class DirectiveInjector implements DirectiveBinder {
Key _key8 = null; dynamic _obj8; List<Key> _pKeys8; Function _factory8;
Key _key9 = null; dynamic _obj9; List<Key> _pKeys9; Function _factory9;

@Deprecated("Deprecated. Use getFromParent instead.")
Object get parent => this._parent;

static _toVisId(Visibility v) => identical(v, Visibility.LOCAL)
? VISIBILITY_LOCAL
: (identical(v, Visibility.CHILDREN) ? VISIBILITY_CHILDREN : VISIBILITY_DIRECT_CHILD);
Expand All @@ -140,12 +143,12 @@ class DirectiveInjector implements DirectiveBinder {

static Binding _tempBinding = new Binding();

DirectiveInjector(parent, appInjector, this._node, this._nodeAttrs, this._eventHandler,
this.scope, this._animate)
DirectiveInjector(DirectiveInjector parent, Injector appInjector, this._node, this._nodeAttrs,
this._eventHandler, this.scope, this._animate)
: appInjector = appInjector,
parent = parent == null ? new DefaultDirectiveInjector(appInjector) : parent;
_parent = parent == null ? new DefaultDirectiveInjector(appInjector) : parent;

DirectiveInjector._default(this.parent, this.appInjector)
DirectiveInjector._default(this._parent, this.appInjector)
: _node = null,
_nodeAttrs = null,
_eventHandler = null,
Expand Down Expand Up @@ -193,6 +196,7 @@ class DirectiveInjector implements DirectiveBinder {
}

Object get(Type type) => getByKey(new Key(type));
Object getFromParent(Type type) => _parent.get(type);

Object getByKey(Key key) {
var oldTag = _TAG_GET.makeCurrent();
Expand All @@ -205,6 +209,7 @@ class DirectiveInjector implements DirectiveBinder {
oldTag.makeCurrent();
}
}
Object getFromParentByKey(Key key) => _parent.getByKey(key);

Object _getByKey(Key key) {
int uid = key.uid;
Expand All @@ -228,12 +233,12 @@ class DirectiveInjector implements DirectiveBinder {
} while (false);
switch (visType) {
case VISIBILITY_LOCAL: return appInjector.getByKey(k);
case VISIBILITY_DIRECT_CHILD: return parent._getDirectiveByKey(k, VISIBILITY_LOCAL, i);
case VISIBILITY_CHILDREN: return parent._getDirectiveByKey(k, VISIBILITY_CHILDREN, i);
case VISIBILITY_DIRECT_CHILD: return _parent._getDirectiveByKey(k, VISIBILITY_LOCAL, i);
case VISIBILITY_CHILDREN: return _parent._getDirectiveByKey(k, VISIBILITY_CHILDREN, i);
// SHADOW
case VISIBILITY_COMPONENT_LOCAL: return parent._getDirectiveByKey(k, VISIBILITY_LOCAL, i);
case VISIBILITY_COMPONENT_DIRECT_CHILD: return parent._getDirectiveByKey(k, VISIBILITY_DIRECT_CHILD, i);
case VISIBILITY_COMPONENT_CHILDREN: return parent._getDirectiveByKey(k, VISIBILITY_CHILDREN, i);
case VISIBILITY_COMPONENT_LOCAL: return _parent._getDirectiveByKey(k, VISIBILITY_LOCAL, i);
case VISIBILITY_COMPONENT_DIRECT_CHILD: return _parent._getDirectiveByKey(k, VISIBILITY_DIRECT_CHILD, i);
case VISIBILITY_COMPONENT_CHILDREN: return _parent._getDirectiveByKey(k, VISIBILITY_CHILDREN, i);
default: throw null;
}
}
Expand Down Expand Up @@ -265,7 +270,7 @@ class DirectiveInjector implements DirectiveBinder {
case ELEMENT_PROBE_KEY_ID: return elementProbe;
case NG_ELEMENT_KEY_ID: return ngElement;
case EVENT_HANDLER_KEY_ID: return _eventHandler;
case CONTENT_PORT_KEY_ID: return parent._getById(keyId);
case CONTENT_PORT_KEY_ID: return _parent._getById(keyId);
default: new NoProviderError(_KEYS[keyId]);
}
}
Expand Down Expand Up @@ -324,7 +329,7 @@ class DirectiveInjector implements DirectiveBinder {

ElementProbe get elementProbe {
if (_elementProbe == null) {
ElementProbe parentProbe = parent is DirectiveInjector ? parent.elementProbe : null;
ElementProbe parentProbe = _parent == null ? null : _parent.elementProbe;
_elementProbe = new ElementProbe(parentProbe, _node, this, scope);
}
return _elementProbe;
Expand Down Expand Up @@ -355,7 +360,7 @@ class TemplateDirectiveInjector extends DirectiveInjector {
case VIEW_PORT_KEY_ID: return ((_viewPort) == null) ?
_viewPort = new ViewPort(this, scope, _node, _animate) : _viewPort;
case BOUND_VIEW_FACTORY_KEY_ID: return (_boundViewFactory == null) ?
_boundViewFactory = _viewFactory.bind(this.parent) : _boundViewFactory;
_boundViewFactory = _viewFactory.bind(_parent) : _boundViewFactory;
default: return super._getById(keyId);
}
}
Expand Down Expand Up @@ -411,8 +416,7 @@ class ShadowDomComponentDirectiveInjector extends ComponentDirectiveInjector {

ElementProbe get elementProbe {
if (_elementProbe == null) {
ElementProbe parentProbe =
parent is DirectiveInjector ? parent.elementProbe : parent.getByKey(ELEMENT_PROBE_KEY);
ElementProbe parentProbe = _parent == null ? null : _parent.elementProbe;
_elementProbe = new ElementProbe(parentProbe, _shadowRoot, this, scope);
}
return _elementProbe;
Expand All @@ -427,7 +431,7 @@ class DefaultDirectiveInjector extends DirectiveInjector {

Object getByKey(Key key) => appInjector.getByKey(key);
_getDirectiveByKey(Key key, int visType, Injector i) =>
parent == null ? i.getByKey(key) : parent._getDirectiveByKey(key, visType, i);
_parent == null ? i.getByKey(key) : _parent._getDirectiveByKey(key, visType, i);
_getById(int keyId) {
switch (keyId) {
case CONTENT_PORT_KEY_ID: return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/directive/ng_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class NgControl implements AttachAware, DetachAware {

NgControl(NgElement this._element, DirectiveInjector injector,
Animate this._animate)
: _parentControl = injector.parent.getByKey(NG_CONTROL_KEY);
: _parentControl = injector.getFromParentByKey(NG_CONTROL_KEY);

@override
void attach() {
Expand Down
2 changes: 1 addition & 1 deletion lib/routing/ng_bind_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NgBindRoute implements RouteProvider {
NgBindRoute(this._router, this._injector, NgRoutingHelper _);

/// Returns the parent [RouteProvider].
RouteProvider get _parent => _injector.parent.getByKey(ROUTE_PROVIDER_KEY);
RouteProvider get _parent => _injector.getFromParentByKey(ROUTE_PROVIDER_KEY);

Route get route => routeName.startsWith('.') ?
_parent.route.getRoute(routeName.substring(1)) :
Expand Down
2 changes: 1 addition & 1 deletion lib/routing/ng_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class NgView implements DetachAware, RouteProvider {
: _dirInjector = dirInjector,
_locationService = dirInjector.getByKey(NG_ROUTING_HELPER_KEY)
{
RouteProvider routeProvider = dirInjector.parent.getByKey(NG_VIEW_KEY);
RouteProvider routeProvider = dirInjector.getFromParentByKey(NG_VIEW_KEY);
_route = routeProvider != null ?
routeProvider.route.newHandle() :
router.root.newHandle();
Expand Down
12 changes: 11 additions & 1 deletion test/core_dom/directive_injector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library directive_injector_spec;

import '../_specs.dart';
import 'package:angular/core_dom/directive_injector.dart';
import 'package:angular/core_dom/static_keys.dart';

void main() {
describe('DirectiveInjector', () {
Expand Down Expand Up @@ -33,7 +34,6 @@ void main() {
});

it('should return basic types', () {
expect(injector.parent is DefaultDirectiveInjector).toBe(true);
expect(injector.appInjector).toBe(appInjector);
expect(injector.scope).toBe(scope);
expect(injector.get(Injector)).toBe(appInjector);
Expand All @@ -47,6 +47,16 @@ void main() {
expect((injector.get(ElementProbe) as ElementProbe).element).toBe(div);
});

it('should support get from parent methods', () {
var newDiv = new DivElement();
var childInjector = new DirectiveInjector(
injector, appInjector, newDiv, new NodeAttrs(newDiv), eventHandler, scope, animate);

expect(childInjector.get(Node)).toBe(newDiv);
expect(childInjector.getFromParent(Node)).toBe(div);
expect(childInjector.getFromParentByKey(NODE_KEY)).toBe(div);
});

it('should instantiate types', () {
addDirective(_Type9);
addDirective(_Type8);
Expand Down

0 comments on commit 3b7b0d6

Please sign in to comment.