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

Commit f8bbd35

Browse files
committed
feat(DirectiveInjector): add a toInstanceOf parameter to bind()
1 parent bb16d92 commit f8bbd35

File tree

7 files changed

+26
-32
lines changed

7 files changed

+26
-32
lines changed

lib/core/annotation_src.dart

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ library angular.core.annotation_src;
33
import "package:di/di.dart" show Injector, Visibility, Factory;
44

55
abstract class DirectiveBinder {
6-
bind(key, {Function toFactory, inject, Visibility visibility: Visibility.CHILDREN});
6+
void bind(key, {dynamic toValue,
7+
Function toFactory,
8+
Type toImplementation,
9+
toInstanceOf,
10+
inject: const[],
11+
Visibility visibility: Visibility.LOCAL});
712
}
813

914
typedef void DirectiveBinderFn(DirectiveBinder module);

lib/core_dom/directive_injector.dart

+13-11
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class DirectiveInjector implements DirectiveBinder {
9999
, EVENT_HANDLER_KEY
100100
, KEEP_ME_LAST
101101
];
102-
102+
103103
final DirectiveInjector parent;
104104
final Injector appInjector;
105105
final Node _node;
@@ -138,7 +138,7 @@ class DirectiveInjector implements DirectiveBinder {
138138
}
139139
}
140140

141-
static Binding _temp_binding = new Binding();
141+
static Binding _tempBinding = new Binding();
142142

143143
DirectiveInjector(parent, appInjector, this._node, this._nodeAttrs, this._eventHandler,
144144
this.scope, this._animate)
@@ -152,21 +152,23 @@ class DirectiveInjector implements DirectiveBinder {
152152
scope = null,
153153
_animate = null;
154154

155-
bind(key, {dynamic toValue: DEFAULT_VALUE,
155+
void bind(key, {dynamic toValue: DEFAULT_VALUE,
156156
Function toFactory: DEFAULT_VALUE,
157-
Type toImplementation, inject: const[],
157+
Type toImplementation,
158+
toInstanceOf,
159+
inject: const[],
158160
Visibility visibility: Visibility.LOCAL}) {
159161
if (key == null) throw 'Key is required';
160162
if (key is! Key) key = new Key(key);
161163
if (inject is! List) inject = [inject];
162164

163-
_temp_binding.bind(key, Module.DEFAULT_REFLECTOR, toValue: toValue, toFactory: toFactory,
164-
toImplementation: toImplementation, inject: inject);
165+
_tempBinding.bind(key, Module.DEFAULT_REFLECTOR, toValue: toValue, toFactory: toFactory,
166+
toImplementation: toImplementation, inject: inject, toInstanceOf: toInstanceOf);
165167

166-
bindByKey(key, _temp_binding.factory, _temp_binding.parameterKeys, visibility);
168+
bindByKey(key, _tempBinding.factory, _tempBinding.parameterKeys, visibility);
167169
}
168170

169-
bindByKey(Key key, Function factory, List<Key> parameterKeys, [Visibility visibility]) {
171+
void bindByKey(Key key, Function factory, List<Key> parameterKeys, [Visibility visibility]) {
170172
if (visibility == null) visibility = Visibility.CHILDREN;
171173
int visibilityId = _toVisId(visibility);
172174
int keyVisId = key.uid;
@@ -211,7 +213,7 @@ class DirectiveInjector implements DirectiveBinder {
211213
return isDirective ? _getDirectiveByKey(key, uid, appInjector) : _getById(uid);
212214
}
213215

214-
_getDirectiveByKey(Key k, int visType, Injector i) {
216+
Object _getDirectiveByKey(Key k, int visType, Injector i) {
215217
do {
216218
if (_key0 == null) break; if (identical(_key0, k)) return _obj0 == null ? _obj0 = _new(_pKeys0, _factory0) : _obj0;
217219
if (_key1 == null) break; if (identical(_key1, k)) return _obj1 == null ? _obj1 = _new(_pKeys1, _factory1) : _obj1;
@@ -340,7 +342,7 @@ class TemplateDirectiveInjector extends DirectiveInjector {
340342
final ViewFactory _viewFactory;
341343
ViewPort _viewPort;
342344
BoundViewFactory _boundViewFactory;
343-
345+
344346
TemplateDirectiveInjector(DirectiveInjector parent, Injector appInjector,
345347
Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
346348
Scope scope, Animate animate, this._viewFactory)
@@ -357,7 +359,7 @@ class TemplateDirectiveInjector extends DirectiveInjector {
357359
default: return super._getById(keyId);
358360
}
359361
}
360-
362+
361363
}
362364

363365
abstract class ComponentDirectiveInjector extends DirectiveInjector {

lib/core_dom/element_binder.dart

-14
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@ class TemplateElementBinder extends ElementBinder {
2323
String toString() => "[TemplateElementBinder template:$template]";
2424
}
2525

26-
// TODO: This class exists for forwards API compatibility only.
27-
// Remove it after migration to DI 2.0.
28-
class _DirectiveBinderImpl implements DirectiveBinder {
29-
final module = new Module();
30-
31-
_DirectiveBinderImpl();
32-
33-
bind(key, {Function toFactory: DEFAULT_VALUE, List inject: null,
34-
Visibility visibility: Directive.LOCAL_VISIBILITY}) {
35-
module.bind(key, toFactory: toFactory, inject: inject,
36-
visibility: visibility);
37-
}
38-
}
39-
4026
/**
4127
* ElementBinder is created by the Selector and is responsible for instantiating
4228
* individual directives and binding element properties.

lib/directive/ng_form.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ part of angular.directive;
2020
map: const { 'ng-form': '@name' })
2121
class NgForm extends NgControl {
2222
static module(DirectiveBinder binder) =>
23-
binder.bind(NgControl, inject: NG_FORM_KEY, visibility: Visibility.CHILDREN);
23+
binder.bind(NgControl, toInstanceOf: NG_FORM_KEY, visibility: Visibility.CHILDREN);
2424

2525
final Scope _scope;
2626

lib/routing/ng_bind_route.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class NgBindRoute implements RouteProvider {
3131
final Router _router;
3232
final DirectiveInjector _injector;
3333

34-
static void module(DirectiveBinder binder)
35-
=> binder.bind(RouteProvider, inject: NG_BIND_ROUTE_KEY, visibility: Visibility.CHILDREN);
34+
static void module(DirectiveBinder binder) =>
35+
binder.bind(RouteProvider, toInstanceOf: NG_BIND_ROUTE_KEY, visibility: Visibility.CHILDREN);
3636

3737
// We inject NgRoutingHelper to force initialization of routing.
3838
NgBindRoute(this._router, this._injector, NgRoutingHelper _);

lib/routing/ng_view.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ part of angular.routing;
5656
module: NgView.module,
5757
visibility: Visibility.CHILDREN)
5858
class NgView implements DetachAware, RouteProvider {
59-
static void module(DirectiveBinder binder)
60-
=> binder.bind(RouteProvider, inject: NG_VIEW_KEY, visibility: Visibility.CHILDREN);
59+
static void module(DirectiveBinder binder) =>
60+
binder.bind(RouteProvider, toInstanceOf: NG_VIEW_KEY, visibility: Visibility.CHILDREN);
6161

6262
final NgRoutingHelper _locationService;
6363
final ViewCache _viewCache;

test/core_dom/compiler_spec.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,8 @@ class PublishModuleDirectiveSuperType {
10751075
selector: '[publish-types]',
10761076
module: PublishModuleAttrDirective.module)
10771077
class PublishModuleAttrDirective implements PublishModuleDirectiveSuperType {
1078-
static module(i) => i.bind(PublishModuleDirectiveSuperType, inject: PublishModuleAttrDirective);
1078+
static module(i) =>
1079+
i.bind(PublishModuleDirectiveSuperType, toInstanceOf: PublishModuleAttrDirective);
10791080

10801081
static DirectiveInjector _injector;
10811082
PublishModuleAttrDirective(DirectiveInjector injector) {

0 commit comments

Comments
 (0)