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

Commit

Permalink
feat(compiler): Backport DirectiveBinder API from #1178 to allow
Browse files Browse the repository at this point in the history
gradual migration.

Originally authored by @yjbanov
  • Loading branch information
jbdeboer committed Jul 14, 2014
1 parent 49b7032 commit 1f3cca4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/core/annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export "package:angular/core/annotation_src.dart" show
ShadowRootAware,

Formatter,
DirectiveBinder,
DirectiveBinderFn,
Injectable,

Directive,
Expand Down
7 changes: 7 additions & 0 deletions lib/core/annotation_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ library angular.core.annotation_src;

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

abstract class DirectiveBinder {
bind(key, {Function toFactory, inject,
Visibility visibility: Directive.CHILDREN_VISIBILITY});
}

typedef void DirectiveBinderFn(DirectiveBinder module);

RegExp _ATTR_NAME = new RegExp(r'\[([^\]]+)\]$');

const String SHADOW_DOM_INJECTOR_NAME = 'SHADOW_INJECTOR';
Expand Down
25 changes: 23 additions & 2 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ class TemplateElementBinder extends ElementBinder {
}
}

// TODO: This class exists for forwards API compatibility only.
// Remove it after migration to DI 2.0.
class _DirectiveBinderImpl implements DirectiveBinder {
final module = new Module();

_DirectiveBinderImpl();

bind(key, {Function toFactory: DEFAULT_VALUE, List inject: null,
Visibility visibility: Directive.LOCAL_VISIBILITY}) {
module.bind(key, toFactory: toFactory, inject: inject,
visibility: visibility);
}
}

/**
* ElementBinder is created by the Selector and is responsible for instantiating
Expand Down Expand Up @@ -297,8 +310,16 @@ class ElementBinder {

_createDirectiveFactories(ref, nodeModule, node, nodesAttrsDirectives, nodeAttrs,
visibility);
if (ref.annotation.module != null) {
nodeModule.install(ref.annotation.module());
// Choose between old-style Module-based API and new-style DirectiveBinder-base API
var moduleFn = ref.annotation.module;
if (moduleFn != null) {
if (moduleFn is DirectiveBinderFn) {
var binder = new _DirectiveBinderImpl();
moduleFn(binder);
nodeModule.install(binder.module);
} else {
nodeModule.install(moduleFn());
}
}
});

Expand Down
3 changes: 2 additions & 1 deletion lib/core_dom/module_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import 'package:perf_api/perf_api.dart';
import 'package:angular/cache/module.dart';

import 'package:angular/core/annotation.dart';
import 'package:angular/core/annotation_src.dart' show SHADOW_DOM_INJECTOR_NAME;
import 'package:angular/core/annotation_src.dart'
show SHADOW_DOM_INJECTOR_NAME, DirectiveBinder, DirectiveBinderFn;
import 'package:angular/core/module_internal.dart';
import 'package:angular/core/parser/parser.dart';
import 'package:angular/core_dom/dom_util.dart' as util;
Expand Down
2 changes: 2 additions & 0 deletions test/angular_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ main() {
"angular.core.annotation_src.DetachAware",
"angular.core.annotation_src.Directive",
"angular.core.annotation_src.DirectiveAnnotation",
"angular.core.annotation_src.DirectiveBinder",
"angular.core.annotation_src.DirectiveBinderFn",
"angular.core.annotation_src.Formatter",
"angular.core.annotation_src.Injectable",
"angular.core.annotation_src.NgAttr",
Expand Down

0 comments on commit 1f3cca4

Please sign in to comment.