Skip to content

Commit b727dcd

Browse files
jbdeboerDiana Salsbury
authored andcommitted
feat(compiler): Backport DirectiveBinder API from dart-archive#1178 to allow
gradual migration. Originally authored by @yjbanov
1 parent d1b7d0f commit b727dcd

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

lib/core/annotation.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export "package:angular/core/annotation_src.dart" show
1111
ShadowRootAware,
1212

1313
Formatter,
14+
DirectiveBinder,
15+
DirectiveBinderFn,
1416
Injectable,
1517

1618
Directive,

lib/core/annotation_src.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ library angular.core.annotation_src;
22

33
import "package:di/di.dart" show Injector, Visibility;
44

5+
abstract class DirectiveBinder {
6+
bind(key, {Function toFactory, inject,
7+
Visibility visibility: Directive.CHILDREN_VISIBILITY});
8+
}
9+
10+
typedef void DirectiveBinderFn(DirectiveBinder module);
11+
512
RegExp _ATTR_NAME = new RegExp(r'\[([^\]]+)\]$');
613

714
const String SHADOW_DOM_INJECTOR_NAME = 'SHADOW_INJECTOR';

lib/core_dom/element_binder.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ class TemplateElementBinder extends ElementBinder {
3333
}
3434
}
3535

36+
// TODO: This class exists for forwards API compatibility only.
37+
// Remove it after migration to DI 2.0.
38+
class _DirectiveBinderImpl implements DirectiveBinder {
39+
final module = new Module();
40+
41+
_DirectiveBinderImpl();
42+
43+
bind(key, {Function toFactory: DEFAULT_VALUE, List inject: null,
44+
Visibility visibility: Directive.LOCAL_VISIBILITY}) {
45+
module.bind(key, toFactory: toFactory, inject: inject,
46+
visibility: visibility);
47+
}
48+
}
3649

3750
/**
3851
* ElementBinder is created by the Selector and is responsible for instantiating
@@ -298,8 +311,16 @@ class ElementBinder {
298311

299312
_createDirectiveFactories(ref, nodeModule, node, nodesAttrsDirectives, nodeAttrs,
300313
visibility);
301-
if (ref.annotation.module != null) {
302-
nodeModule.install(ref.annotation.module());
314+
// Choose between old-style Module-based API and new-style DirectiveBinder-base API
315+
var moduleFn = ref.annotation.module;
316+
if (moduleFn != null) {
317+
if (moduleFn is DirectiveBinderFn) {
318+
var binder = new _DirectiveBinderImpl();
319+
moduleFn(binder);
320+
nodeModule.install(binder.module);
321+
} else {
322+
nodeModule.install(moduleFn());
323+
}
303324
}
304325
});
305326

lib/core_dom/module_internal.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import 'package:perf_api/perf_api.dart';
1111
import 'package:angular/cache/module.dart';
1212

1313
import 'package:angular/core/annotation.dart';
14-
import 'package:angular/core/annotation_src.dart' show SHADOW_DOM_INJECTOR_NAME;
14+
import 'package:angular/core/annotation_src.dart'
15+
show SHADOW_DOM_INJECTOR_NAME, DirectiveBinder, DirectiveBinderFn;
1516
import 'package:angular/core/module_internal.dart';
1617
import 'package:angular/core/parser/parser.dart';
1718
import 'package:angular/core_dom/dom_util.dart' as util;
@@ -98,4 +99,4 @@ class CoreDomModule extends Module {
9899
bind(NgElement);
99100
bind(EventHandler);
100101
}
101-
}
102+
}

test/angular_spec.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ main() {
104104
"angular.core.annotation_src.DetachAware",
105105
"angular.core.annotation_src.Directive",
106106
"angular.core.annotation_src.DirectiveAnnotation",
107+
"angular.core.annotation_src.DirectiveBinder",
108+
"angular.core.annotation_src.DirectiveBinderFn",
107109
"angular.core.annotation_src.Formatter",
108110
"angular.core.annotation_src.Injectable",
109111
"angular.core.annotation_src.NgAttr",

0 commit comments

Comments
 (0)