From 6f889e30945a7aa8b0564fc6951ca2bfd732c00d Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 25 Nov 2014 18:52:42 -0800 Subject: [PATCH] feat(di): add OpaqueToken to DI Using `new Object()` as a token causes cryptic errors. OpaqueToken class should be used instead. --- modules/core/src/application.js | 18 +++++++++--------- modules/di/src/di.js | 1 + modules/di/src/opaque_token.js | 11 +++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 modules/di/src/opaque_token.js diff --git a/modules/core/src/application.js b/modules/core/src/application.js index 915bf217e4..2d4d050dc3 100644 --- a/modules/core/src/application.js +++ b/modules/core/src/application.js @@ -1,4 +1,4 @@ -import {Injector, bind} from 'di/di'; +import {Injector, bind, OpaqueToken} from 'di/di'; import {Type, FIELD, isBlank, isPresent, BaseException} from 'facade/lang'; import {DOM, Element} from 'facade/dom'; import {Compiler} from './compiler/compiler'; @@ -21,11 +21,11 @@ var _rootBindings = [ bind(Reflector).toValue(reflector), Compiler, TemplateLoader, DirectiveMetadataReader, Parser, Lexer ]; -export var appViewToken = new Object(); -export var appWatchGroupToken = new Object(); -export var appElementToken = new Object(); -export var appComponentAnnotatedTypeToken = new Object(); -export var appDocumentToken = new Object(); +export var appViewToken = new OpaqueToken('AppView'); +export var appRecordRangeToken = new OpaqueToken('AppRecordRange'); +export var appElementToken = new OpaqueToken('AppElement'); +export var appComponentAnnotatedTypeToken = new OpaqueToken('AppComponentAnnotatedType'); +export var appDocumentToken = new OpaqueToken('AppDocument'); // Exported only for tests that need to overwrite default document binding. export function documentDependentBindings(appComponentType) { @@ -59,10 +59,10 @@ export function documentDependentBindings(appComponentType) { }); }, [Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken]), - bind(appWatchGroupToken).toFactory((rootView) => rootView.recordRange, + bind(appRecordRangeToken).toFactory((rootView) => rootView.recordRange, [appViewToken]), - bind(ChangeDetector).toFactory((appWatchGroup) => - new ChangeDetector(appWatchGroup), [appWatchGroupToken]) + bind(ChangeDetector).toFactory((appRecordRange) => + new ChangeDetector(appRecordRange), [appRecordRangeToken]) ]; } diff --git a/modules/di/src/di.js b/modules/di/src/di.js index 1f380b60de..d80c89132e 100644 --- a/modules/di/src/di.js +++ b/modules/di/src/di.js @@ -4,3 +4,4 @@ export * from './binding'; export * from './key'; export * from './module'; export * from './exceptions'; +export * from './opaque_token'; diff --git a/modules/di/src/opaque_token.js b/modules/di/src/opaque_token.js new file mode 100644 index 0000000000..9706bce696 --- /dev/null +++ b/modules/di/src/opaque_token.js @@ -0,0 +1,11 @@ +export class OpaqueToken { + _desc:string; + + constructor(desc:string){ + this._desc = `Token(${desc})`; + } + + toString() { + return this._desc; + } +} \ No newline at end of file