diff --git a/docs/typescript-definition-package/index.js b/docs/typescript-definition-package/index.js index 55b1ccdd40..afad0317c4 100644 --- a/docs/typescript-definition-package/index.js +++ b/docs/typescript-definition-package/index.js @@ -72,7 +72,7 @@ module.exports = new Package('angular-v2-docs', [jsdocPackage, nunjucksPackage, { id: 'angular2/test_lib', references: ['./angular2.d.ts'], - remapTypes: { Type: 'ng.Type', Binding: 'ng.Binding', ViewMetadata: 'ng.ViewMetadata', Injector: 'ng.Injector', + remapTypes: { Type: 'ng.Type', Binding: 'ng.Binding', Provider: 'ng.Provider', ViewMetadata: 'ng.ViewMetadata', Injector: 'ng.Injector', Predicate: 'ng.Predicate', ElementRef: 'ng.ElementRef', DebugElement: 'ng.DebugElement', InjectableReference: 'ng.InjectableReference', ComponentRef: 'ng.ComponentRef' }, modules: {'angular2/test_lib': {namespace: 'ngTestLib', id: 'angular2/test_lib'}} diff --git a/modules/angular2/angular2.dart b/modules/angular2/angular2.dart index 155fe6295c..7a4a73ae17 100644 --- a/modules/angular2/angular2.dart +++ b/modules/angular2/angular2.dart @@ -8,5 +8,5 @@ library angular2; export 'package:angular2/core.dart' hide forwardRef, resolveForwardRef, ForwardRefFn; export 'package:angular2/profile.dart'; export 'package:angular2/lifecycle_hooks.dart'; -export 'package:angular2/src/core/application_tokens.dart' hide APP_COMPONENT_REF_PROMISE, APP_ID_RANDOM_BINDING; +export 'package:angular2/src/core/application_tokens.dart' hide APP_COMPONENT_REF_PROMISE, APP_ID_RANDOM_PROVIDER; export 'package:angular2/src/core/render/dom/dom_tokens.dart'; diff --git a/modules/angular2/docs/di/di.md b/modules/angular2/docs/di/di.md index b5cbe3c7b4..5d34efa936 100644 --- a/modules/angular2/docs/di/di.md +++ b/modules/angular2/docs/di/di.md @@ -108,7 +108,7 @@ while (inj) { inj = inj.parent; } } -throw new NoBindingError(requestedKey); +throw new NoProviderError(requestedKey); ``` So in the following example @@ -160,7 +160,7 @@ var child = parent.resolveAndCreateChild([ bind(Engine).toClass(TurboEngine) ]); -parent.get(Car); // will throw NoBindingError +parent.get(Car); // will throw NoProviderError ``` diff --git a/modules/angular2/http.ts b/modules/angular2/http.ts index 4942a0c6aa..0f2f8e34e5 100644 --- a/modules/angular2/http.ts +++ b/modules/angular2/http.ts @@ -4,7 +4,7 @@ * The http module provides services to perform http requests. To get started, see the {@link Http} * class. */ -import {bind, Binding} from 'angular2/core'; +import {provide, Provider} from 'angular2/core'; import {Http, Jsonp} from './src/http/http'; import {XHRBackend, XHRConnection} from './src/http/backends/xhr_backend'; import {JSONPBackend, JSONPBackend_, JSONPConnection} from './src/http/backends/jsonp_backend'; @@ -40,18 +40,18 @@ export {URLSearchParams} from './src/http/url_search_params'; /** * Provides a basic set of injectables to use the {@link Http} service in any application. * - * The `HTTP_BINDINGS` should be included either in a component's injector, + * The `HTTP_PROVIDERS` should be included either in a component's injector, * or in the root injector when bootstrapping an application. * * ### Example ([live demo](http://plnkr.co/edit/snj7Nv?p=preview)) * * ``` * import {bootstrap, Component, NgFor, View} from 'angular2/angular2'; - * import {HTTP_BINDINGS, Http} from 'angular2/http'; + * import {HTTP_PROVIDERS, Http} from 'angular2/http'; * * @Component({ * selector: 'app', - * bindings: [HTTP_BINDINGS] + * providers: [HTTP_PROVIDERS] * }) * @View({ * template: ` @@ -83,11 +83,11 @@ export {URLSearchParams} from './src/http/url_search_params'; * .catch(err => console.error(err)); * ``` * - * The primary public API included in `HTTP_BINDINGS` is the {@link Http} class. - * However, other bindings required by `Http` are included, + * The primary public API included in `HTTP_PROVIDERS` is the {@link Http} class. + * However, other providers required by `Http` are included, * which may be beneficial to override in certain cases. * - * The bindings included in `HTTP_BINDINGS` include: + * The providers included in `HTTP_PROVIDERS` include: * * {@link Http} * * {@link XHRBackend} * * `BrowserXHR` - Private factory to create `XMLHttpRequest` instances @@ -96,38 +96,38 @@ export {URLSearchParams} from './src/http/url_search_params'; * * There may be cases where it makes sense to extend the base request options, * such as to add a search string to be appended to all URLs. - * To accomplish this, a new binding for {@link RequestOptions} should - * be added in the same injector as `HTTP_BINDINGS`. + * To accomplish this, a new provider for {@link RequestOptions} should + * be added in the same injector as `HTTP_PROVIDERS`. * * ### Example ([live demo](http://plnkr.co/edit/aCMEXi?p=preview)) * * ``` - * import {bind, bootstrap} from 'angular2/angular2'; - * import {HTTP_BINDINGS, BaseRequestOptions, RequestOptions} from 'angular2/http'; + * import {provide, bootstrap} from 'angular2/angular2'; + * import {HTTP_PROVIDERS, BaseRequestOptions, RequestOptions} from 'angular2/http'; * * class MyOptions extends BaseRequestOptions { * search: string = 'coreTeam=true'; * } * - * bootstrap(App, [HTTP_BINDINGS, bind(RequestOptions).toClass(MyOptions)]) + * bootstrap(App, [HTTP_PROVIDERS, provide(RequestOptions, {asClass: MyOptions})]) * .catch(err => console.error(err)); * ``` * * Likewise, to use a mock backend for unit tests, the {@link XHRBackend} - * binding should be bound to {@link MockBackend}. + * provider should be bound to {@link MockBackend}. * * ### Example ([live demo](http://plnkr.co/edit/7LWALD?p=preview)) * * ``` - * import {bind, Injector} from 'angular2/angular2'; - * import {HTTP_BINDINGS, Http, Response, XHRBackend, MockBackend} from 'angular2/http'; + * import {provide, Injector} from 'angular2/angular2'; + * import {HTTP_PROVIDERS, Http, Response, XHRBackend, MockBackend} from 'angular2/http'; * * var people = [{name: 'Jeff'}, {name: 'Tobias'}]; * * var injector = Injector.resolveAndCreate([ - * HTTP_BINDINGS, + * HTTP_PROVIDERS, * MockBackend, - * bind(XHRBackend).toAlias(MockBackend) + * provide(XHRBackend, {asAlias: MockBackend}) * ]); * var http = injector.get(Http); * var backend = injector.get(MockBackend); @@ -150,34 +150,40 @@ export {URLSearchParams} from './src/http/url_search_params'; * }); * ``` */ -export const HTTP_BINDINGS: any[] = [ +export const HTTP_PROVIDERS: any[] = [ // TODO(pascal): use factory type annotations once supported in DI // issue: https://github.com/angular/angular/issues/3183 - bind(Http) - .toFactory((xhrBackend, requestOptions) => { return new Http(xhrBackend, requestOptions);}, - [XHRBackend, RequestOptions]), + provide(Http, + { + asFactory: (xhrBackend, requestOptions) => new Http(xhrBackend, requestOptions), + deps: [XHRBackend, RequestOptions] + }), BrowserXhr, - bind(RequestOptions).toClass(BaseRequestOptions), - bind(ResponseOptions).toClass(BaseResponseOptions), + provide(RequestOptions, {asClass: BaseRequestOptions}), + provide(ResponseOptions, {asClass: BaseResponseOptions}), XHRBackend ]; +/** + * @deprecated + */ +export const HTTP_BINDINGS = HTTP_PROVIDERS; /** - * Provides a basic set of bindings to use the {@link Jsonp} service in any application. + * Provides a basic set of providers to use the {@link Jsonp} service in any application. * - * The `JSONP_BINDINGS` should be included either in a component's injector, + * The `JSONP_PROVIDERS` should be included either in a component's injector, * or in the root injector when bootstrapping an application. * * ### Example ([live demo](http://plnkr.co/edit/vmeN4F?p=preview)) * * ``` * import {Component, NgFor, View} from 'angular2/angular2'; - * import {JSONP_BINDINGS, Jsonp} from 'angular2/http'; + * import {JSONP_PROVIDERS, Jsonp} from 'angular2/http'; * * @Component({ * selector: 'app', - * bindings: [JSONP_BINDINGS] + * providers: [JSONP_PROVIDERS] * }) * @View({ * template: ` @@ -202,11 +208,11 @@ export const HTTP_BINDINGS: any[] = [ * } * ``` * - * The primary public API included in `JSONP_BINDINGS` is the {@link Jsonp} class. - * However, other bindings required by `Jsonp` are included, + * The primary public API included in `JSONP_PROVIDERS` is the {@link Jsonp} class. + * However, other providers required by `Jsonp` are included, * which may be beneficial to override in certain cases. * - * The bindings included in `JSONP_BINDINGS` include: + * The providers included in `JSONP_PROVIDERS` include: * * {@link Jsonp} * * {@link JSONPBackend} * * `BrowserJsonp` - Private factory @@ -215,37 +221,37 @@ export const HTTP_BINDINGS: any[] = [ * * There may be cases where it makes sense to extend the base request options, * such as to add a search string to be appended to all URLs. - * To accomplish this, a new binding for {@link RequestOptions} should - * be added in the same injector as `JSONP_BINDINGS`. + * To accomplish this, a new provider for {@link RequestOptions} should + * be added in the same injector as `JSONP_PROVIDERS`. * * ### Example ([live demo](http://plnkr.co/edit/TFug7x?p=preview)) * * ``` - * import {bind, bootstrap} from 'angular2/angular2'; - * import {JSONP_BINDINGS, BaseRequestOptions, RequestOptions} from 'angular2/http'; + * import {provide, bootstrap} from 'angular2/angular2'; + * import {JSONP_PROVIDERS, BaseRequestOptions, RequestOptions} from 'angular2/http'; * * class MyOptions extends BaseRequestOptions { * search: string = 'coreTeam=true'; * } * - * bootstrap(App, [JSONP_BINDINGS, bind(RequestOptions).toClass(MyOptions)]) + * bootstrap(App, [JSONP_PROVIDERS, provide(RequestOptions, {asClass: MyOptions})]) * .catch(err => console.error(err)); * ``` * * Likewise, to use a mock backend for unit tests, the {@link JSONPBackend} - * binding should be bound to {@link MockBackend}. + * provider should be bound to {@link MockBackend}. * * ### Example ([live demo](http://plnkr.co/edit/HDqZWL?p=preview)) * * ``` - * import {bind, Injector} from 'angular2/angular2'; - * import {JSONP_BINDINGS, Jsonp, Response, JSONPBackend, MockBackend} from 'angular2/http'; + * import {provide, Injector} from 'angular2/angular2'; + * import {JSONP_PROVIDERS, Jsonp, Response, JSONPBackend, MockBackend} from 'angular2/http'; * * var people = [{name: 'Jeff'}, {name: 'Tobias'}]; * var injector = Injector.resolveAndCreate([ - * JSONP_BINDINGS, + * JSONP_PROVIDERS, * MockBackend, - * bind(JSONPBackend).toAlias(MockBackend) + * provide(JSONPBackend, {asAlias: MockBackend}) * ]); * var jsonp = injector.get(Jsonp); * var backend = injector.get(MockBackend); @@ -268,15 +274,21 @@ export const HTTP_BINDINGS: any[] = [ * }); * ``` */ -export const JSONP_BINDINGS: any[] = [ +export const JSONP_PROVIDERS: any[] = [ // TODO(pascal): use factory type annotations once supported in DI // issue: https://github.com/angular/angular/issues/3183 - bind(Jsonp) - .toFactory( - (jsonpBackend, requestOptions) => { return new Jsonp(jsonpBackend, requestOptions);}, - [JSONPBackend, RequestOptions]), + provide(Jsonp, + { + asFactory: (jsonpBackend, requestOptions) => new Jsonp(jsonpBackend, requestOptions), + deps: [JSONPBackend, RequestOptions] + }), BrowserJsonp, - bind(RequestOptions).toClass(BaseRequestOptions), - bind(ResponseOptions).toClass(BaseResponseOptions), - bind(JSONPBackend).toClass(JSONPBackend_) + provide(RequestOptions, {asClass: BaseRequestOptions}), + provide(ResponseOptions, {asClass: BaseResponseOptions}), + provide(JSONPBackend, {asClass: JSONPBackend_}) ]; + +/** + * @deprecated + */ +export const JSON_BINDINGS = JSONP_PROVIDERS; \ No newline at end of file diff --git a/modules/angular2/router.ts b/modules/angular2/router.ts index 6304478ebc..967a785f93 100644 --- a/modules/angular2/router.ts +++ b/modules/angular2/router.ts @@ -28,7 +28,7 @@ import {RouterOutlet} from './src/router/router_outlet'; import {RouterLink} from './src/router/router_link'; import {RouteRegistry} from './src/router/route_registry'; import {Location} from './src/router/location'; -import {bind, OpaqueToken, Binding} from './core'; +import {provide, OpaqueToken, Provider} from './core'; import {CONST_EXPR} from './src/core/facade/lang'; import {ApplicationRef} from './src/core/application_ref'; import {BaseException} from 'angular2/src/core/facade/exceptions'; @@ -44,7 +44,7 @@ import {BaseException} from 'angular2/src/core/facade/exceptions'; * import {Component, View} from 'angular2/angular2'; * import { * ROUTER_DIRECTIVES, - * ROUTER_BINDINGS, + * ROUTER_PROVIDERS, * RouteConfig * } from 'angular2/router'; * @@ -57,7 +57,7 @@ import {BaseException} from 'angular2/src/core/facade/exceptions'; * // ... * } * - * bootstrap(AppCmp, [ROUTER_BINDINGS]); + * bootstrap(AppCmp, [ROUTER_PROVIDERS]); * ``` */ export const ROUTER_PRIMARY_COMPONENT: OpaqueToken = @@ -72,7 +72,7 @@ export const ROUTER_PRIMARY_COMPONENT: OpaqueToken = * * ``` * import {Component, View} from 'angular2/angular2'; - * import {ROUTER_DIRECTIVES, ROUTER_BINDINGS, RouteConfig} from 'angular2/router'; + * import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from 'angular2/router'; * * @Component({...}) * @View({directives: [ROUTER_DIRECTIVES]}) @@ -83,13 +83,13 @@ export const ROUTER_PRIMARY_COMPONENT: OpaqueToken = * // ... * } * - * bootstrap(AppCmp, ROUTER_BINDINGS); + * bootstrap(AppCmp, [ROUTER_PROVIDERS]); * ``` */ export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]); /** - * A list of {@link Binding}s. To use the router, you must add this to your application. + * A list of {@link Provider}s. To use the router, you must add this to your application. * * ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm)) * @@ -97,7 +97,7 @@ export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]); * import {Component, View} from 'angular2/angular2'; * import { * ROUTER_DIRECTIVES, - * ROUTER_BINDINGS, + * ROUTER_PROVIDERS, * RouteConfig * } from 'angular2/router'; * @@ -110,23 +110,29 @@ export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]); * // ... * } * - * bootstrap(AppCmp, [ROUTER_BINDINGS]); + * bootstrap(AppCmp, [ROUTER_PROVIDERS]); * ``` */ -export const ROUTER_BINDINGS: any[] = CONST_EXPR([ +export const ROUTER_PROVIDERS: any[] = CONST_EXPR([ RouteRegistry, - CONST_EXPR(new Binding(LocationStrategy, {toClass: PathLocationStrategy})), + CONST_EXPR(new Provider(LocationStrategy, {toClass: PathLocationStrategy})), Location, - CONST_EXPR(new Binding(Router, - { - toFactory: routerFactory, - deps: CONST_EXPR([RouteRegistry, Location, ROUTER_PRIMARY_COMPONENT]) - })), - CONST_EXPR(new Binding( + CONST_EXPR( + new Provider(Router, + { + toFactory: routerFactory, + deps: CONST_EXPR([RouteRegistry, Location, ROUTER_PRIMARY_COMPONENT]) + })), + CONST_EXPR(new Provider( ROUTER_PRIMARY_COMPONENT, {toFactory: routerPrimaryComponentFactory, deps: CONST_EXPR([ApplicationRef])})) ]); +/** + * @deprecated + */ +export const ROUTER_BINDINGS = ROUTER_PROVIDERS; + function routerFactory(registry, location, primaryComponent) { return new RootRouter(registry, location, primaryComponent); } diff --git a/modules/angular2/src/core/application.dart b/modules/angular2/src/core/application.dart index f0f6f84f3d..a338922584 100644 --- a/modules/angular2/src/core/application.dart +++ b/modules/angular2/src/core/application.dart @@ -18,11 +18,11 @@ export 'package:angular2/src/core/linker/dynamic_component_loader.dart' show Com /// /// See [commonBootstrap] for detailed documentation. Future bootstrap(Type appComponentType, - [List componentInjectableBindings]) { + [List componentInjectableProviders]) { reflector.reflectionCapabilities = new ReflectionCapabilities(); - var bindings = [compilerBindings()]; - if (componentInjectableBindings != null) { - bindings.add(componentInjectableBindings); + var providers = [compilerProviders()]; + if (componentInjectableProviders != null) { + providers.add(componentInjectableProviders); } - return commonBootstrap(appComponentType, bindings); + return commonBootstrap(appComponentType, providers); } diff --git a/modules/angular2/src/core/application.ts b/modules/angular2/src/core/application.ts index 2dc9a0a12f..7c6383554f 100644 --- a/modules/angular2/src/core/application.ts +++ b/modules/angular2/src/core/application.ts @@ -1,8 +1,8 @@ // Public API for Application -import {Binding} from './di'; +import {Provider} from './di'; import {Type, isPresent} from 'angular2/src/core/facade/lang'; import {Promise} from 'angular2/src/core/facade/async'; -import {compilerBindings} from 'angular2/src/core/compiler/compiler'; +import {compilerProviders} from 'angular2/src/core/compiler/compiler'; import {commonBootstrap} from './application_common'; import {ComponentRef} from './linker/dynamic_component_loader'; @@ -19,9 +19,9 @@ export { /// See [commonBootstrap] for detailed documentation. export function bootstrap(appComponentType: /*Type*/ any, - appBindings: Array = null): + appBindings: Array = null): Promise { - var bindings = [compilerBindings()]; + var bindings = [compilerProviders()]; if (isPresent(appBindings)) { bindings.push(appBindings); } diff --git a/modules/angular2/src/core/application_common.ts b/modules/angular2/src/core/application_common.ts index 887469a50f..9e5aa6a4b7 100644 --- a/modules/angular2/src/core/application_common.ts +++ b/modules/angular2/src/core/application_common.ts @@ -1,5 +1,5 @@ -import {FORM_BINDINGS} from 'angular2/src/core/forms'; -import {bind, Binding, Injector, OpaqueToken} from 'angular2/src/core/di'; +import {FORM_PROVIDERS} from 'angular2/src/core/forms'; +import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di'; import { NumberWrapper, Type, @@ -34,37 +34,36 @@ import { SharedStylesHost, DomSharedStylesHost } from 'angular2/src/core/render/dom/shared_styles_host'; -import {EXCEPTION_BINDING} from './platform_bindings'; +import {EXCEPTION_PROVIDER} from './platform_bindings'; import {AnimationBuilder} from 'angular2/src/animate/animation_builder'; import {BrowserDetails} from 'angular2/src/animate/browser_details'; import {wtfInit} from './profile/wtf_init'; import {platformCommon, PlatformRef, applicationCommonBindings} from './application_ref'; /** - * A default set of bindings which apply only to an Angular application running on + * A default set of providers which apply only to an Angular application running on * the UI thread. */ -export function applicationDomBindings(): Array { +export function applicationDomBindings(): Array { if (isBlank(DOM)) { throw "Must set a root DOM adapter first."; } return [ - bind(DOCUMENT) - .toValue(DOM.defaultDoc()), + provide(DOCUMENT, {asValue: DOM.defaultDoc()}), EventManager, - new Binding(EVENT_MANAGER_PLUGINS, {toClass: DomEventsPlugin, multi: true}), - new Binding(EVENT_MANAGER_PLUGINS, {toClass: KeyEventsPlugin, multi: true}), - new Binding(EVENT_MANAGER_PLUGINS, {toClass: HammerGesturesPlugin, multi: true}), - bind(DomRenderer).toClass(DomRenderer_), - bind(Renderer).toAlias(DomRenderer), + new Provider(EVENT_MANAGER_PLUGINS, {toClass: DomEventsPlugin, multi: true}), + new Provider(EVENT_MANAGER_PLUGINS, {toClass: KeyEventsPlugin, multi: true}), + new Provider(EVENT_MANAGER_PLUGINS, {toClass: HammerGesturesPlugin, multi: true}), + provide(DomRenderer, {asClass: DomRenderer_}), + provide(Renderer, {asAlias: DomRenderer}), DomSharedStylesHost, - bind(SharedStylesHost).toAlias(DomSharedStylesHost), - EXCEPTION_BINDING, - bind(XHR).toValue(new XHRImpl()), + provide(SharedStylesHost, {asAlias: DomSharedStylesHost}), + EXCEPTION_PROVIDER, + provide(XHR, {asValue: new XHRImpl()}), Testability, BrowserDetails, AnimationBuilder, - FORM_BINDINGS + FORM_PROVIDERS ]; } @@ -73,25 +72,25 @@ export function applicationDomBindings(): Array { * * See {@link PlatformRef} for details on the Angular platform. * - * # Without specified bindings + * # Without specified providers * - * If no bindings are specified, `platform`'s behavior depends on whether an existing + * If no providers are specified, `platform`'s behavior depends on whether an existing * platform exists: * * If no platform exists, a new one will be created with the default {@link platformBindings}. * - * If a platform already exists, it will be returned (regardless of what bindings it + * If a platform already exists, it will be returned (regardless of what providers it * was created with). This is a convenience feature, allowing for multiple applications * to be loaded into the same platform without awareness of each other. * - * # With specified bindings + * # With specified providers * - * It is also possible to specify bindings to be made in the new platform. These bindings + * It is also possible to specify providers to be made in the new platform. These providers * will be shared between all applications on the page. For example, an abstraction for * the browser cookie jar should be bound at the platform level, because there is only one * cookie jar regardless of how many applications on the age will be accessing it. * - * If bindings are specified directly, `platform` will create the Angular platform with + * If providers are specified directly, `platform` will create the Angular platform with * them if a platform did not exist already. If it did exist, however, an error will be * thrown. * @@ -101,7 +100,7 @@ export function applicationDomBindings(): Array { * DOM access. Web-worker applications should call `platform` from * `src/web_workers/worker/application_common` instead. */ -export function platform(bindings?: Array): PlatformRef { +export function platform(bindings?: Array): PlatformRef { return platformCommon(bindings, () => { BrowserDomAdapter.makeCurrent(); wtfInit(); @@ -129,10 +128,10 @@ export function platform(bindings?: Array): PlatformRef * ``` * * An application is bootstrapped inside an existing browser DOM, typically `index.html`. - * Unlike Angular 1, Angular 2 does not compile/process bindings in `index.html`. This is + * Unlike Angular 1, Angular 2 does not compile/process providers in `index.html`. This is * mainly for security reasons, as well as architectural changes in Angular 2. This means * that `index.html` can safely be processed using server-side technologies such as - * bindings. Bindings can thus use double-curly `{{ syntax }}` without collision from + * providers. Bindings can thus use double-curly `{{ syntax }}` without collision from * Angular 2 component double-curly `{{ syntax }}`. * * We can use this script code: @@ -170,7 +169,7 @@ export function platform(bindings?: Array): PlatformRef * 4. It creates a shadow DOM on the selected component's host element and loads the * template into it. * 5. It instantiates the specified component. - * 6. Finally, Angular performs change detection to apply the initial data bindings for the + * 6. Finally, Angular performs change detection to apply the initial data providers for the * application. * * @@ -214,7 +213,7 @@ export function platform(bindings?: Array): PlatformRef * # API * - `appComponentType`: The root component which should act as the application. This is * a reference to a `Type` which is annotated with `@Component(...)`. - * - `componentInjectableBindings`: An additional set of bindings that can be added to the + * - `componentInjectableBindings`: An additional set of providers that can be added to the * app injector to override default injection behavior. * - `errorReporter`: `function(exception:any, stackTrace:string)` a default error reporter * for unhandled exceptions. @@ -222,7 +221,7 @@ export function platform(bindings?: Array): PlatformRef * Returns a `Promise` of {@link ComponentRef}. */ export function commonBootstrap(appComponentType: /*Type*/ any, - appBindings: Array = null): + appBindings: Array = null): Promise { var p = platform(); var bindings = [applicationCommonBindings(), applicationDomBindings()]; diff --git a/modules/angular2/src/core/application_ref.ts b/modules/angular2/src/core/application_ref.ts index ae5df71de6..2f2a0a8cba 100644 --- a/modules/angular2/src/core/application_ref.ts +++ b/modules/angular2/src/core/application_ref.ts @@ -1,10 +1,10 @@ import {NgZone} from 'angular2/src/core/zone/ng_zone'; import {Type, isBlank, isPresent, assertionsEnabled} from 'angular2/src/core/facade/lang'; -import {bind, Binding, Injector, OpaqueToken} from 'angular2/src/core/di'; +import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di'; import { APP_COMPONENT_REF_PROMISE, APP_COMPONENT, - APP_ID_RANDOM_BINDING + APP_ID_RANDOM_PROVIDER } from './application_tokens'; import {Promise, PromiseWrapper, PromiseCompleter} from 'angular2/src/core/facade/async'; import {ListWrapper} from 'angular2/src/core/facade/collection'; @@ -44,67 +44,71 @@ import {AppViewManager_} from "./linker/view_manager"; import {Compiler_} from "./linker/compiler"; /** - * Constructs the set of bindings meant for use at the platform level. + * Constructs the set of providers meant for use at the platform level. * - * These are bindings that should be singletons shared among all Angular applications + * These are providers that should be singletons shared among all Angular applications * running on the page. */ -export function platformBindings(): Array { - return [bind(Reflector).toValue(reflector), TestabilityRegistry]; +export function platformBindings(): Array { + return [provide(Reflector, {asValue: reflector}), TestabilityRegistry]; } /** - * Construct bindings specific to an individual root component. + * Construct providers specific to an individual root component. */ -function _componentBindings(appComponentType: Type): Array { +function _componentProviders(appComponentType: Type): Array { return [ - bind(APP_COMPONENT) - .toValue(appComponentType), - bind(APP_COMPONENT_REF_PROMISE) - .toFactory( - (dynamicComponentLoader, injector: Injector) => { - // TODO(rado): investigate whether to support bindings on root component. - return dynamicComponentLoader.loadAsRoot(appComponentType, null, injector) - .then((componentRef) => { - if (isPresent(componentRef.location.nativeElement)) { - injector.get(TestabilityRegistry) - .registerApplication(componentRef.location.nativeElement, - injector.get(Testability)); - } - return componentRef; - }); - }, - [DynamicComponentLoader, Injector]), - - bind(appComponentType) - .toFactory((p: Promise) => p.then(ref => ref.instance), [APP_COMPONENT_REF_PROMISE]), + provide(APP_COMPONENT, {asValue: appComponentType}), + provide(APP_COMPONENT_REF_PROMISE, + { + asFactory: (dynamicComponentLoader, injector: Injector) => { + // TODO(rado): investigate whether to support bindings on root component. + return dynamicComponentLoader.loadAsRoot(appComponentType, null, injector) + .then((componentRef) => { + if (isPresent(componentRef.location.nativeElement)) { + injector.get(TestabilityRegistry) + .registerApplication(componentRef.location.nativeElement, + injector.get(Testability)); + } + return componentRef; + }); + }, + deps: [DynamicComponentLoader, Injector] + }), + provide(appComponentType, + { + asFactory: (p: Promise) => p.then(ref => ref.instance), + deps: [APP_COMPONENT_REF_PROMISE] + }), ]; } /** - * Construct a default set of bindings which should be included in any Angular + * Construct a default set of providers which should be included in any Angular * application, regardless of whether it runs on the UI thread or in a web worker. */ -export function applicationCommonBindings(): Array { +export function applicationCommonBindings(): Array { return [ - bind(Compiler) - .toClass(Compiler_), - APP_ID_RANDOM_BINDING, + provide(Compiler, {asClass: Compiler_}), + APP_ID_RANDOM_PROVIDER, AppViewPool, - bind(APP_VIEW_POOL_CAPACITY).toValue(10000), - bind(AppViewManager).toClass(AppViewManager_), + provide(APP_VIEW_POOL_CAPACITY, {asValue: 10000}), + provide(AppViewManager, {asClass: AppViewManager_}), AppViewManagerUtils, AppViewListener, ProtoViewFactory, ViewResolver, DEFAULT_PIPES, - bind(IterableDiffers).toValue(defaultIterableDiffers), - bind(KeyValueDiffers).toValue(defaultKeyValueDiffers), + provide(IterableDiffers, {asValue: defaultIterableDiffers}), + provide(KeyValueDiffers, {asValue: defaultKeyValueDiffers}), DirectiveResolver, PipeResolver, - bind(DynamicComponentLoader).toClass(DynamicComponentLoader_), - bind(LifeCycle).toFactory((exceptionHandler) => new LifeCycle_(null, assertionsEnabled()), - [ExceptionHandler]), + provide(DynamicComponentLoader, {asClass: DynamicComponentLoader_}), + provide(LifeCycle, + { + asFactory: (exceptionHandler) => new LifeCycle_(null, assertionsEnabled()), + deps: [ExceptionHandler] + }) ]; } @@ -117,7 +121,7 @@ export function createNgZone(): NgZone { var _platform: PlatformRef; -export function platformCommon(bindings?: Array, initializer?: () => void): +export function platformCommon(bindings?: Array, initializer?: () => void): PlatformRef { if (isPresent(_platform)) { if (isBlank(bindings)) { @@ -148,7 +152,7 @@ export function platformCommon(bindings?: Array, initial export abstract class PlatformRef { /** * Retrieve the platform {@link Injector}, which is the parent injector for - * every Angular application on the page and provides singleton bindings. + * every Angular application on the page and provides singleton providers. */ get injector(): Injector { return unimplemented(); }; @@ -163,10 +167,10 @@ export abstract class PlatformRef { * * # Application Bindings * - * Angular applications require numerous bindings to be properly instantiated. - * When using `application()` to create a new app on the page, these bindings + * Angular applications require numerous providers to be properly instantiated. + * When using `application()` to create a new app on the page, these providers * must be provided. Fortunately, there are helper functions to configure - * typical bindings, as shown in the example below. + * typical providers, as shown in the example below. * * # Example * ``` @@ -180,21 +184,21 @@ export abstract class PlatformRef { * * See the {@link bootstrap} documentation for more details. */ - abstract application(bindings: Array): ApplicationRef; + abstract application(bindings: Array): ApplicationRef; /** - * Instantiate a new Angular application on the page, using bindings which + * Instantiate a new Angular application on the page, using providers which * are only available asynchronously. One such use case is to initialize an * application running in a web worker. * * # Usage * * `bindingFn` is a function that will be called in the new application's zone. - * It should return a {@link Promise} to a list of bindings to be used for the + * It should return a {@link Promise} to a list of providers to be used for the * new application. Once this promise resolves, the application will be * constructed in the same manner as a normal `application()`. */ - abstract asyncApplication(bindingFn: (zone: NgZone) => Promise>): + abstract asyncApplication(bindingFn: (zone: NgZone) => Promise>): Promise; /** @@ -211,33 +215,33 @@ export class PlatformRef_ extends PlatformRef { get injector(): Injector { return this._injector; } - application(bindings: Array): ApplicationRef { + application(bindings: Array): ApplicationRef { var app = this._initApp(createNgZone(), bindings); return app; } asyncApplication(bindingFn: (zone: NgZone) => - Promise>): Promise { + Promise>): Promise { var zone = createNgZone(); var completer = PromiseWrapper.completer(); zone.run(() => { - PromiseWrapper.then(bindingFn(zone), (bindings: Array) => { + PromiseWrapper.then(bindingFn(zone), (bindings: Array) => { completer.resolve(this._initApp(zone, bindings)); }); }); return completer.promise; } - private _initApp(zone: NgZone, bindings: Array): ApplicationRef { + private _initApp(zone: NgZone, providers: Array): ApplicationRef { var injector: Injector; var app: ApplicationRef; zone.run(() => { - bindings.push(bind(NgZone).toValue(zone)); - bindings.push(bind(ApplicationRef).toFactory((): ApplicationRef => app, [])); + providers.push(provide(NgZone, {asValue: zone})); + providers.push(provide(ApplicationRef, {asFactory: (): ApplicationRef => app, deps: []})); var exceptionHandler; try { - injector = this.injector.resolveAndCreateChild(bindings); + injector = this.injector.resolveAndCreateChild(providers); exceptionHandler = injector.get(ExceptionHandler); zone.overrideOnErrorHandler((e, s) => exceptionHandler.call(e, s)); } catch (e) { @@ -285,18 +289,18 @@ export abstract class ApplicationRef { * * # Optional Bindings * - * Bindings for the given component can optionally be overridden via the `bindings` - * parameter. These bindings will only apply for the root component being added and any + * Bindings for the given component can optionally be overridden via the `providers` + * parameter. These providers will only apply for the root component being added and any * child components under it. * * # Example * ``` * var app = platform.application([applicationCommonBindings(), applicationDomBindings()]; * app.bootstrap(FirstRootComponent); - * app.bootstrap(SecondRootComponent, [bind(OverrideBinding).toClass(OverriddenBinding)]); + * app.bootstrap(SecondRootComponent, [provide(OverrideBinding, {asClass: OverriddenBinding})]); * ``` */ - abstract bootstrap(componentType: Type, bindings?: Array): + abstract bootstrap(componentType: Type, bindings?: Array): Promise; /** @@ -333,17 +337,18 @@ export class ApplicationRef_ extends ApplicationRef { this._bootstrapListeners.push(listener); } - bootstrap(componentType: Type, bindings?: Array): Promise { + bootstrap(componentType: Type, + providers?: Array): Promise { var completer = PromiseWrapper.completer(); this._zone.run(() => { - var componentBindings = _componentBindings(componentType); - if (isPresent(bindings)) { - componentBindings.push(bindings); + var componentProviders = _componentProviders(componentType); + if (isPresent(providers)) { + componentProviders.push(providers); } var exceptionHandler = this._injector.get(ExceptionHandler); this._rootComponentTypes.push(componentType); try { - var injector: Injector = this._injector.resolveAndCreateChild(componentBindings); + var injector: Injector = this._injector.resolveAndCreateChild(componentProviders); var compRefToken: Promise = injector.get(APP_COMPONENT_REF_PROMISE); var tick = (componentRef) => { var appChangeDetector = internalView(componentRef.hostView).changeDetector; diff --git a/modules/angular2/src/core/application_tokens.ts b/modules/angular2/src/core/application_tokens.ts index f7e802e440..e8cc58a5a7 100644 --- a/modules/angular2/src/core/application_tokens.ts +++ b/modules/angular2/src/core/application_tokens.ts @@ -1,4 +1,4 @@ -import {OpaqueToken, Binding} from 'angular2/src/core/di'; +import {OpaqueToken, Provider} from 'angular2/src/core/di'; import {CONST_EXPR, Math, StringWrapper} from 'angular2/src/core/facade/lang'; /** @@ -31,20 +31,20 @@ export const APP_COMPONENT: OpaqueToken = CONST_EXPR(new OpaqueToken('AppCompone * {@link ViewEncapsulation#Emulated} is being used. * * If you need to avoid randomly generated value to be used as an application id, you can provide - * a custom value via a DI binding configuring the root {@link Injector} + * a custom value via a DI provider configuring the root {@link Injector} * using this token. */ export const APP_ID: OpaqueToken = CONST_EXPR(new OpaqueToken('AppId')); -function _appIdRandomBindingFactory() { +function _appIdRandomProviderFactory() { return `${_randomChar()}${_randomChar()}${_randomChar()}`; } /** * Bindings that will generate a random APP_ID_TOKEN. */ -export const APP_ID_RANDOM_BINDING: Binding = - CONST_EXPR(new Binding(APP_ID, {toFactory: _appIdRandomBindingFactory, deps: []})); +export const APP_ID_RANDOM_PROVIDER: Provider = + CONST_EXPR(new Provider(APP_ID, {toFactory: _appIdRandomProviderFactory, deps: []})); function _randomChar(): string { return StringWrapper.fromCharCode(97 + Math.floor(Math.random() * 25)); diff --git a/modules/angular2/src/core/change_detection/change_detector_ref.ts b/modules/angular2/src/core/change_detection/change_detector_ref.ts index a10d213ab3..bd779ff2d7 100644 --- a/modules/angular2/src/core/change_detection/change_detector_ref.ts +++ b/modules/angular2/src/core/change_detection/change_detector_ref.ts @@ -85,7 +85,7 @@ export abstract class ChangeDetectorRef { * } * * @Component({ - * selector: 'app', bindings: [DataProvider] + * selector: 'app', providers: [DataProvider] * }) * @View({ * template: ` @@ -166,7 +166,7 @@ export abstract class ChangeDetectorRef { * * @Component({ * selector: 'app', - * bindings: [DataProvider] + * providers: [DataProvider] * }) * @View({ * template: ` diff --git a/modules/angular2/src/core/change_detection/differs/iterable_differs.ts b/modules/angular2/src/core/change_detection/differs/iterable_differs.ts index c80b591bc1..6ab03370e4 100644 --- a/modules/angular2/src/core/change_detection/differs/iterable_differs.ts +++ b/modules/angular2/src/core/change_detection/differs/iterable_differs.ts @@ -2,7 +2,7 @@ import {isBlank, isPresent, CONST} from 'angular2/src/core/facade/lang'; import {BaseException} from 'angular2/src/core/facade/exceptions'; import {ListWrapper} from 'angular2/src/core/facade/collection'; import {ChangeDetectorRef} from '../change_detector_ref'; -import {Binding, SkipSelfMetadata, OptionalMetadata, Injectable} from 'angular2/src/core/di'; +import {Provider, SkipSelfMetadata, OptionalMetadata, Injectable} from 'angular2/src/core/di'; export interface IterableDiffer { diff(object: Object): any; @@ -36,7 +36,7 @@ export class IterableDiffers { } /** - * Takes an array of {@link IterableDifferFactory} and returns a binding used to extend the + * Takes an array of {@link IterableDifferFactory} and returns a provider used to extend the * inherited {@link IterableDiffers} instance with the provided factories and return a new * {@link IterableDiffers} instance. * @@ -48,14 +48,14 @@ export class IterableDiffers { * * ``` * @Component({ - * viewBindings: [ + * viewProviders: [ * IterableDiffers.extend([new ImmutableListDiffer()]) * ] * }) * ``` */ - static extend(factories: IterableDifferFactory[]): Binding { - return new Binding(IterableDiffers, { + static extend(factories: IterableDifferFactory[]): Provider { + return new Provider(IterableDiffers, { toFactory: (parent: IterableDiffers) => { if (isBlank(parent)) { // Typically would occur when calling IterableDiffers.extend inside of dependencies passed diff --git a/modules/angular2/src/core/change_detection/differs/keyvalue_differs.ts b/modules/angular2/src/core/change_detection/differs/keyvalue_differs.ts index cbda4fe6bc..17ca4879e8 100644 --- a/modules/angular2/src/core/change_detection/differs/keyvalue_differs.ts +++ b/modules/angular2/src/core/change_detection/differs/keyvalue_differs.ts @@ -2,7 +2,7 @@ import {isBlank, isPresent, CONST} from 'angular2/src/core/facade/lang'; import {BaseException} from 'angular2/src/core/facade/exceptions'; import {ListWrapper} from 'angular2/src/core/facade/collection'; import {ChangeDetectorRef} from '../change_detector_ref'; -import {Binding, SkipSelfMetadata, OptionalMetadata, Injectable} from 'angular2/src/core/di'; +import {Provider, SkipSelfMetadata, OptionalMetadata, Injectable} from 'angular2/src/core/di'; export interface KeyValueDiffer { diff(object: Object); @@ -36,7 +36,7 @@ export class KeyValueDiffers { } /** - * Takes an array of {@link KeyValueDifferFactory} and returns a binding used to extend the + * Takes an array of {@link KeyValueDifferFactory} and returns a provider used to extend the * inherited {@link KeyValueDiffers} instance with the provided factories and return a new * {@link KeyValueDiffers} instance. * @@ -48,14 +48,14 @@ export class KeyValueDiffers { * * ``` * @Component({ - * viewBindings: [ + * viewProviders: [ * KeyValueDiffers.extend([new ImmutableMapDiffer()]) * ] * }) * ``` */ - static extend(factories: KeyValueDifferFactory[]): Binding { - return new Binding(KeyValueDiffers, { + static extend(factories: KeyValueDifferFactory[]): Provider { + return new Provider(KeyValueDiffers, { toFactory: (parent: KeyValueDiffers) => { if (isBlank(parent)) { // Typically would occur when calling KeyValueDiffers.extend inside of dependencies passed diff --git a/modules/angular2/src/core/compiler/compiler.ts b/modules/angular2/src/core/compiler/compiler.ts index f4a5769163..8787a8bb44 100644 --- a/modules/angular2/src/core/compiler/compiler.ts +++ b/modules/angular2/src/core/compiler/compiler.ts @@ -8,7 +8,7 @@ export { export {SourceModule, SourceWithImports} from './source_module'; import {assertionsEnabled, Type} from 'angular2/src/core/facade/lang'; -import {bind, Binding} from 'angular2/src/core/di'; +import {provide, Provider} from 'angular2/src/core/di'; import {TemplateParser} from 'angular2/src/core/compiler/template_parser'; import {HtmlParser} from 'angular2/src/core/compiler/html_parser'; import {TemplateNormalizer} from 'angular2/src/core/compiler/template_normalizer'; @@ -29,7 +29,7 @@ import {AppRootUrl} from 'angular2/src/core/compiler/app_root_url'; import {AnchorBasedAppRootUrl} from 'angular2/src/core/compiler/anchor_based_app_root_url'; import {Parser, Lexer} from 'angular2/src/core/change_detection/change_detection'; -export function compilerBindings(): Array { +export function compilerProviders(): Array { return [ Lexer, Parser, @@ -40,16 +40,18 @@ export function compilerBindings(): Array { StyleCompiler, CommandCompiler, ChangeDetectionCompiler, - bind(ChangeDetectorGenConfig) - .toValue( - new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled(), false, true)), + provide(ChangeDetectorGenConfig, + { + asValue: + new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled(), false, true) + }), TemplateCompiler, - bind(RuntimeCompiler).toClass(RuntimeCompiler_), - bind(Compiler).toAlias(RuntimeCompiler), + provide(RuntimeCompiler, {asClass: RuntimeCompiler_}), + provide(Compiler, {asAlias: RuntimeCompiler}), DomElementSchemaRegistry, - bind(ElementSchemaRegistry).toAlias(DomElementSchemaRegistry), + provide(ElementSchemaRegistry, {asAlias: DomElementSchemaRegistry}), AnchorBasedAppRootUrl, - bind(AppRootUrl).toAlias(AnchorBasedAppRootUrl), + provide(AppRootUrl, {asAlias: AnchorBasedAppRootUrl}), UrlResolver ]; } diff --git a/modules/angular2/src/core/debug.ts b/modules/angular2/src/core/debug.ts index 47e910330d..aca1a83a94 100644 --- a/modules/angular2/src/core/debug.ts +++ b/modules/angular2/src/core/debug.ts @@ -1,2 +1,6 @@ export {DebugElement, asNativeElements, By, Scope, inspectElement} from './debug/debug_element'; -export {inspectNativeElement, ELEMENT_PROBE_BINDINGS} from './debug/debug_element_view_listener'; +export { + inspectNativeElement, + ELEMENT_PROBE_PROVIDERS, + ELEMENT_PROBE_BINDINGS +} from './debug/debug_element_view_listener'; diff --git a/modules/angular2/src/core/debug/debug_element_view_listener.ts b/modules/angular2/src/core/debug/debug_element_view_listener.ts index 13220876de..c94128a889 100644 --- a/modules/angular2/src/core/debug/debug_element_view_listener.ts +++ b/modules/angular2/src/core/debug/debug_element_view_listener.ts @@ -1,6 +1,6 @@ import {CONST_EXPR, isPresent, NumberWrapper, StringWrapper} from 'angular2/src/core/facade/lang'; import {MapWrapper, Map, ListWrapper} from 'angular2/src/core/facade/collection'; -import {Injectable, bind, Binding} from 'angular2/src/core/di'; +import {Injectable, provide, Provider} from 'angular2/src/core/di'; import {AppViewListener} from 'angular2/src/core/linker/view_listener'; import {AppView} from 'angular2/src/core/linker/view'; import {DOM} from 'angular2/src/core/dom/dom_adapter'; @@ -67,7 +67,9 @@ export class DebugElementViewListener implements AppViewListener { } } -export const ELEMENT_PROBE_BINDINGS: any[] = CONST_EXPR([ +export const ELEMENT_PROBE_PROVIDERS: any[] = CONST_EXPR([ DebugElementViewListener, - CONST_EXPR(new Binding(AppViewListener, {toAlias: DebugElementViewListener})), + CONST_EXPR(new Provider(AppViewListener, {toAlias: DebugElementViewListener})), ]); + +export const ELEMENT_PROBE_BINDINGS = ELEMENT_PROBE_PROVIDERS; \ No newline at end of file diff --git a/modules/angular2/src/core/di.ts b/modules/angular2/src/core/di.ts index 9eb4b4022b..012640d722 100644 --- a/modules/angular2/src/core/di.ts +++ b/modules/angular2/src/core/di.ts @@ -21,19 +21,23 @@ export {forwardRef, resolveForwardRef, ForwardRefFn} from './di/forward_ref'; export {Injector} from './di/injector'; export { Binding, - BindingBuilder, + ProviderBuilder, ResolvedBinding, ResolvedFactory, Dependency, - bind -} from './di/binding'; + bind, + + Provider, + ResolvedProvider, + provide +} from './di/provider'; export {Key, TypeLiteral} from './di/key'; export { - NoBindingError, - AbstractBindingError, + NoProviderError, + AbstractProviderError, CyclicDependencyError, InstantiationError, - InvalidBindingError, + InvalidProviderError, NoAnnotationError, OutOfBoundsError } from './di/exceptions'; diff --git a/modules/angular2/src/core/di/exceptions.ts b/modules/angular2/src/core/di/exceptions.ts index e67fe218e6..34dc7837e3 100644 --- a/modules/angular2/src/core/di/exceptions.ts +++ b/modules/angular2/src/core/di/exceptions.ts @@ -29,9 +29,9 @@ function constructResolvingPath(keys: any[]): string { /** - * Base class for all errors arising from misconfigured bindings. + * Base class for all errors arising from misconfigured providers. */ -export class AbstractBindingError extends BaseException { +export class AbstractProviderError extends BaseException { /** @internal */ message: string; @@ -63,7 +63,7 @@ export class AbstractBindingError extends BaseException { /** * Thrown when trying to retrieve a dependency by `Key` from {@link Injector}, but the - * {@link Injector} does not have a {@link Binding} for {@link Key}. + * {@link Injector} does not have a {@link Provider} for {@link Key}. * * ### Example ([live demo](http://plnkr.co/edit/vq8D3FRB9aGbnWJqtEPE?p=preview)) * @@ -75,7 +75,7 @@ export class AbstractBindingError extends BaseException { * expect(() => Injector.resolveAndCreate([A])).toThrowError(); * ``` */ -export class NoBindingError extends AbstractBindingError { +export class NoProviderError extends AbstractProviderError { constructor(injector: Injector, key: Key) { super(injector, key, function(keys: any[]) { var first = stringify(ListWrapper.first(keys).token); @@ -91,8 +91,8 @@ export class NoBindingError extends AbstractBindingError { * * ```typescript * var injector = Injector.resolveAndCreate([ - * bind("one").toFactory((two) => "two", [[new Inject("two")]]), - * bind("two").toFactory((one) => "one", [[new Inject("one")]]) + * provide("one", {asFactory: (two) => "two", deps: [[new Inject("two")]]}), + * provide("two", {asFactory: (one) => "one", deps: [[new Inject("one")]]}) * ]); * * expect(() => injector.get("one")).toThrowError(); @@ -100,7 +100,7 @@ export class NoBindingError extends AbstractBindingError { * * Retrieving `A` or `B` throws a `CyclicDependencyError` as the graph above cannot be constructed. */ -export class CyclicDependencyError extends AbstractBindingError { +export class CyclicDependencyError extends AbstractProviderError { constructor(injector: Injector, key: Key) { super(injector, key, function(keys: any[]) { return `Cannot instantiate cyclic dependency!${constructResolvingPath(keys)}`; @@ -163,7 +163,7 @@ export class InstantiationError extends WrappedException { } /** - * Thrown when an object other then {@link Binding} (or `Type`) is passed to {@link Injector} + * Thrown when an object other then {@link Provider} (or `Type`) is passed to {@link Injector} * creation. * * ### Example ([live demo](http://plnkr.co/edit/YatCFbPAMCL0JSSQ4mvH?p=preview)) @@ -172,10 +172,10 @@ export class InstantiationError extends WrappedException { * expect(() => Injector.resolveAndCreate(["not a type"])).toThrowError(); * ``` */ -export class InvalidBindingError extends BaseException { - constructor(binding) { - super("Invalid binding - only instances of Binding and Type are allowed, got: " + - binding.toString()); +export class InvalidProviderError extends BaseException { + constructor(provider) { + super("Invalid provider - only instances of Provider and Type are allowed, got: " + + provider.toString()); } } @@ -246,20 +246,20 @@ export class OutOfBoundsError extends BaseException { // TODO: add a working example after alpha38 is released /** - * Thrown when a multi binding and a regular binding are bound to the same token. + * Thrown when a multi provider and a regular provider are bound to the same token. * * ### Example * * ```typescript * expect(() => Injector.resolveAndCreate([ - * new Binding("Strings", {toValue: "string1", multi: true}), - * new Binding("Strings", {toValue: "string2", multi: false}) + * new Provider("Strings", {toValue: "string1", multi: true}), + * new Provider("Strings", {toValue: "string2", multi: false}) * ])).toThrowError(); * ``` */ -export class MixingMultiBindingsWithRegularBindings extends BaseException { - constructor(binding1, binding2) { - super("Cannot mix multi bindings and regular bindings, got: " + binding1.toString() + " " + - binding2.toString()); +export class MixingMultiProvidersWithRegularProvidersError extends BaseException { + constructor(provider1, provider2) { + super("Cannot mix multi providers and regular providers, got: " + provider1.toString() + " " + + provider2.toString()); } } diff --git a/modules/angular2/src/core/di/injector.ts b/modules/angular2/src/core/di/injector.ts index 198ffa86f2..ee90f6c20d 100644 --- a/modules/angular2/src/core/di/injector.ts +++ b/modules/angular2/src/core/di/injector.ts @@ -1,21 +1,20 @@ import {Map, MapWrapper, ListWrapper} from 'angular2/src/core/facade/collection'; import { - ResolvedBinding, - Binding, + ResolvedProvider, + Provider, Dependency, - BindingBuilder, + ProviderBuilder, ResolvedFactory, - bind, - resolveBindings -} from './binding'; + provide, + resolveProviders +} from './provider'; import { - AbstractBindingError, - NoBindingError, + AbstractProviderError, + NoProviderError, CyclicDependencyError, InstantiationError, - InvalidBindingError, - OutOfBoundsError, - MixingMultiBindingsWithRegularBindings + InvalidProviderError, + OutOfBoundsError } from './exceptions'; import {FunctionWrapper, Type, isPresent, isBlank, CONST_EXPR} from 'angular2/src/core/facade/lang'; import {Key} from './key'; @@ -28,19 +27,19 @@ const _MAX_CONSTRUCTION_COUNTER = 10; export const UNDEFINED: Object = CONST_EXPR(new Object()); /** - * Visibility of a {@link Binding}. + * Visibility of a {@link Provider}. */ export enum Visibility { /** - * A `Public` {@link Binding} is only visible to regular (as opposed to host) child injectors. + * A `Public` {@link Provider} is only visible to regular (as opposed to host) child injectors. */ Public, /** - * A `Private` {@link Binding} is only visible to host (as opposed to regular) child injectors. + * A `Private` {@link Provider} is only visible to host (as opposed to regular) child injectors. */ Private, /** - * A `PublicAndPrivate` {@link Binding} is visible to both host and regular child injectors. + * A `PublicAndPrivate` {@link Provider} is visible to both host and regular child injectors. */ PublicAndPrivate } @@ -52,21 +51,21 @@ function canSee(src: Visibility, dst: Visibility): boolean { export interface ProtoInjectorStrategy { - getBindingAtIndex(index: number): ResolvedBinding; + getProviderAtIndex(index: number): ResolvedProvider; createInjectorStrategy(inj: Injector): InjectorStrategy; } export class ProtoInjectorInlineStrategy implements ProtoInjectorStrategy { - binding0: ResolvedBinding = null; - binding1: ResolvedBinding = null; - binding2: ResolvedBinding = null; - binding3: ResolvedBinding = null; - binding4: ResolvedBinding = null; - binding5: ResolvedBinding = null; - binding6: ResolvedBinding = null; - binding7: ResolvedBinding = null; - binding8: ResolvedBinding = null; - binding9: ResolvedBinding = null; + provider0: ResolvedProvider = null; + provider1: ResolvedProvider = null; + provider2: ResolvedProvider = null; + provider3: ResolvedProvider = null; + provider4: ResolvedProvider = null; + provider5: ResolvedProvider = null; + provider6: ResolvedProvider = null; + provider7: ResolvedProvider = null; + provider8: ResolvedProvider = null; + provider9: ResolvedProvider = null; keyId0: number = null; keyId1: number = null; @@ -90,72 +89,72 @@ export class ProtoInjectorInlineStrategy implements ProtoInjectorStrategy { visibility8: Visibility = null; visibility9: Visibility = null; - constructor(protoEI: ProtoInjector, bwv: BindingWithVisibility[]) { + constructor(protoEI: ProtoInjector, bwv: ProviderWithVisibility[]) { var length = bwv.length; if (length > 0) { - this.binding0 = bwv[0].binding; + this.provider0 = bwv[0].provider; this.keyId0 = bwv[0].getKeyId(); this.visibility0 = bwv[0].visibility; } if (length > 1) { - this.binding1 = bwv[1].binding; + this.provider1 = bwv[1].provider; this.keyId1 = bwv[1].getKeyId(); this.visibility1 = bwv[1].visibility; } if (length > 2) { - this.binding2 = bwv[2].binding; + this.provider2 = bwv[2].provider; this.keyId2 = bwv[2].getKeyId(); this.visibility2 = bwv[2].visibility; } if (length > 3) { - this.binding3 = bwv[3].binding; + this.provider3 = bwv[3].provider; this.keyId3 = bwv[3].getKeyId(); this.visibility3 = bwv[3].visibility; } if (length > 4) { - this.binding4 = bwv[4].binding; + this.provider4 = bwv[4].provider; this.keyId4 = bwv[4].getKeyId(); this.visibility4 = bwv[4].visibility; } if (length > 5) { - this.binding5 = bwv[5].binding; + this.provider5 = bwv[5].provider; this.keyId5 = bwv[5].getKeyId(); this.visibility5 = bwv[5].visibility; } if (length > 6) { - this.binding6 = bwv[6].binding; + this.provider6 = bwv[6].provider; this.keyId6 = bwv[6].getKeyId(); this.visibility6 = bwv[6].visibility; } if (length > 7) { - this.binding7 = bwv[7].binding; + this.provider7 = bwv[7].provider; this.keyId7 = bwv[7].getKeyId(); this.visibility7 = bwv[7].visibility; } if (length > 8) { - this.binding8 = bwv[8].binding; + this.provider8 = bwv[8].provider; this.keyId8 = bwv[8].getKeyId(); this.visibility8 = bwv[8].visibility; } if (length > 9) { - this.binding9 = bwv[9].binding; + this.provider9 = bwv[9].provider; this.keyId9 = bwv[9].getKeyId(); this.visibility9 = bwv[9].visibility; } } - getBindingAtIndex(index: number): any { - if (index == 0) return this.binding0; - if (index == 1) return this.binding1; - if (index == 2) return this.binding2; - if (index == 3) return this.binding3; - if (index == 4) return this.binding4; - if (index == 5) return this.binding5; - if (index == 6) return this.binding6; - if (index == 7) return this.binding7; - if (index == 8) return this.binding8; - if (index == 9) return this.binding9; + getProviderAtIndex(index: number): any { + if (index == 0) return this.provider0; + if (index == 1) return this.provider1; + if (index == 2) return this.provider2; + if (index == 3) return this.provider3; + if (index == 4) return this.provider4; + if (index == 5) return this.provider5; + if (index == 6) return this.provider6; + if (index == 7) return this.provider7; + if (index == 8) return this.provider8; + if (index == 9) return this.provider9; throw new OutOfBoundsError(index); } @@ -165,29 +164,29 @@ export class ProtoInjectorInlineStrategy implements ProtoInjectorStrategy { } export class ProtoInjectorDynamicStrategy implements ProtoInjectorStrategy { - bindings: ResolvedBinding[]; + providers: ResolvedProvider[]; keyIds: number[]; visibilities: Visibility[]; - constructor(protoInj: ProtoInjector, bwv: BindingWithVisibility[]) { + constructor(protoInj: ProtoInjector, bwv: ProviderWithVisibility[]) { var len = bwv.length; - this.bindings = ListWrapper.createFixedSize(len); + this.providers = ListWrapper.createFixedSize(len); this.keyIds = ListWrapper.createFixedSize(len); this.visibilities = ListWrapper.createFixedSize(len); for (var i = 0; i < len; i++) { - this.bindings[i] = bwv[i].binding; + this.providers[i] = bwv[i].provider; this.keyIds[i] = bwv[i].getKeyId(); this.visibilities[i] = bwv[i].visibility; } } - getBindingAtIndex(index: number): any { - if (index < 0 || index >= this.bindings.length) { + getProviderAtIndex(index: number): any { + if (index < 0 || index >= this.providers.length) { throw new OutOfBoundsError(index); } - return this.bindings[index]; + return this.providers[index]; } createInjectorStrategy(ei: Injector): InjectorStrategy { @@ -198,16 +197,16 @@ export class ProtoInjectorDynamicStrategy implements ProtoInjectorStrategy { export class ProtoInjector { /** @internal */ _strategy: ProtoInjectorStrategy; - numberOfBindings: number; + numberOfProviders: number; - constructor(bwv: BindingWithVisibility[]) { - this.numberOfBindings = bwv.length; + constructor(bwv: ProviderWithVisibility[]) { + this.numberOfProviders = bwv.length; this._strategy = bwv.length > _MAX_CONSTRUCTION_COUNTER ? new ProtoInjectorDynamicStrategy(this, bwv) : new ProtoInjectorInlineStrategy(this, bwv); } - getBindingAtIndex(index: number): any { return this._strategy.getBindingAtIndex(index); } + getProviderAtIndex(index: number): any { return this._strategy.getProviderAtIndex(index); } } @@ -219,7 +218,7 @@ export interface InjectorStrategy { attach(parent: Injector, isHost: boolean): void; resetConstructionCounter(): void; - instantiateBinding(binding: ResolvedBinding, visibility: Visibility): any; + instantiateProvider(provider: ResolvedProvider, visibility: Visibility): any; } export class InjectorInlineStrategy implements InjectorStrategy { @@ -238,8 +237,8 @@ export class InjectorInlineStrategy implements InjectorStrategy { resetConstructionCounter(): void { this.injector._constructionCounter = 0; } - instantiateBinding(binding: ResolvedBinding, visibility: Visibility): any { - return this.injector._new(binding, visibility); + instantiateProvider(provider: ResolvedProvider, visibility: Visibility): any { + return this.injector._new(provider, visibility); } attach(parent: Injector, isHost: boolean): void { @@ -254,61 +253,61 @@ export class InjectorInlineStrategy implements InjectorStrategy { if (p.keyId0 === keyId && canSee(p.visibility0, visibility)) { if (this.obj0 === UNDEFINED) { - this.obj0 = inj._new(p.binding0, p.visibility0); + this.obj0 = inj._new(p.provider0, p.visibility0); } return this.obj0; } if (p.keyId1 === keyId && canSee(p.visibility1, visibility)) { if (this.obj1 === UNDEFINED) { - this.obj1 = inj._new(p.binding1, p.visibility1); + this.obj1 = inj._new(p.provider1, p.visibility1); } return this.obj1; } if (p.keyId2 === keyId && canSee(p.visibility2, visibility)) { if (this.obj2 === UNDEFINED) { - this.obj2 = inj._new(p.binding2, p.visibility2); + this.obj2 = inj._new(p.provider2, p.visibility2); } return this.obj2; } if (p.keyId3 === keyId && canSee(p.visibility3, visibility)) { if (this.obj3 === UNDEFINED) { - this.obj3 = inj._new(p.binding3, p.visibility3); + this.obj3 = inj._new(p.provider3, p.visibility3); } return this.obj3; } if (p.keyId4 === keyId && canSee(p.visibility4, visibility)) { if (this.obj4 === UNDEFINED) { - this.obj4 = inj._new(p.binding4, p.visibility4); + this.obj4 = inj._new(p.provider4, p.visibility4); } return this.obj4; } if (p.keyId5 === keyId && canSee(p.visibility5, visibility)) { if (this.obj5 === UNDEFINED) { - this.obj5 = inj._new(p.binding5, p.visibility5); + this.obj5 = inj._new(p.provider5, p.visibility5); } return this.obj5; } if (p.keyId6 === keyId && canSee(p.visibility6, visibility)) { if (this.obj6 === UNDEFINED) { - this.obj6 = inj._new(p.binding6, p.visibility6); + this.obj6 = inj._new(p.provider6, p.visibility6); } return this.obj6; } if (p.keyId7 === keyId && canSee(p.visibility7, visibility)) { if (this.obj7 === UNDEFINED) { - this.obj7 = inj._new(p.binding7, p.visibility7); + this.obj7 = inj._new(p.provider7, p.visibility7); } return this.obj7; } if (p.keyId8 === keyId && canSee(p.visibility8, visibility)) { if (this.obj8 === UNDEFINED) { - this.obj8 = inj._new(p.binding8, p.visibility8); + this.obj8 = inj._new(p.provider8, p.visibility8); } return this.obj8; } if (p.keyId9 === keyId && canSee(p.visibility9, visibility)) { if (this.obj9 === UNDEFINED) { - this.obj9 = inj._new(p.binding9, p.visibility9); + this.obj9 = inj._new(p.provider9, p.visibility9); } return this.obj9; } @@ -338,14 +337,14 @@ export class InjectorDynamicStrategy implements InjectorStrategy { objs: any[]; constructor(public protoStrategy: ProtoInjectorDynamicStrategy, public injector: Injector) { - this.objs = ListWrapper.createFixedSize(protoStrategy.bindings.length); + this.objs = ListWrapper.createFixedSize(protoStrategy.providers.length); ListWrapper.fill(this.objs, UNDEFINED); } resetConstructionCounter(): void { this.injector._constructionCounter = 0; } - instantiateBinding(binding: ResolvedBinding, visibility: Visibility): any { - return this.injector._new(binding, visibility); + instantiateProvider(provider: ResolvedProvider, visibility: Visibility): any { + return this.injector._new(provider, visibility); } attach(parent: Injector, isHost: boolean): void { @@ -360,7 +359,7 @@ export class InjectorDynamicStrategy implements InjectorStrategy { for (var i = 0; i < p.keyIds.length; i++) { if (p.keyIds[i] === keyId && canSee(p.visibilities[i], visibility)) { if (this.objs[i] === UNDEFINED) { - this.objs[i] = this.injector._new(p.bindings[i], p.visibilities[i]); + this.objs[i] = this.injector._new(p.providers[i], p.visibilities[i]); } return this.objs[i]; @@ -381,17 +380,17 @@ export class InjectorDynamicStrategy implements InjectorStrategy { getMaxNumberOfObjects(): number { return this.objs.length; } } -export class BindingWithVisibility { - constructor(public binding: ResolvedBinding, public visibility: Visibility){}; +export class ProviderWithVisibility { + constructor(public provider: ResolvedProvider, public visibility: Visibility){}; - getKeyId(): number { return this.binding.key.id; } + getKeyId(): number { return this.provider.key.id; } } /** - * Used to provide dependencies that cannot be easily expressed as bindings. + * Used to provide dependencies that cannot be easily expressed as providers. */ export interface DependencyProvider { - getDependency(injector: Injector, binding: ResolvedBinding, dependency: Dependency): any; + getDependency(injector: Injector, provider: ResolvedProvider, dependency: Dependency): any; } /** @@ -428,10 +427,10 @@ export interface DependencyProvider { */ export class Injector { /** - * Turns an array of binding definitions into an array of resolved bindings. + * Turns an array of provider definitions into an array of resolved providers. * * A resolution is a process of flattening multiple nested arrays and converting individual - * bindings into an array of {@link ResolvedBinding}s. + * providers into an array of {@link ResolvedProvider}s. * * ### Example ([live demo](http://plnkr.co/edit/AiXTHi?p=preview)) * @@ -445,30 +444,30 @@ export class Injector { * constructor(public engine:Engine) {} * } * - * var bindings = Injector.resolve([Car, [[Engine]]]); + * var providers = Injector.resolve([Car, [[Engine]]]); * - * expect(bindings.length).toEqual(2); + * expect(providers.length).toEqual(2); * - * expect(bindings[0] instanceof ResolvedBinding).toBe(true); - * expect(bindings[0].key.displayName).toBe("Car"); - * expect(bindings[0].dependencies.length).toEqual(1); - * expect(bindings[0].factory).toBeDefined(); + * expect(providers[0] instanceof ResolvedProvider).toBe(true); + * expect(providers[0].key.displayName).toBe("Car"); + * expect(providers[0].dependencies.length).toEqual(1); + * expect(providers[0].factory).toBeDefined(); * - * expect(bindings[1].key.displayName).toBe("Engine"); + * expect(providers[1].key.displayName).toBe("Engine"); * }); * ``` * - * See {@link fromResolvedBindings} for more info. + * See {@link fromResolvedProviders} for more info. */ - static resolve(bindings: Array): ResolvedBinding[] { - return resolveBindings(bindings); + static resolve(providers: Array): ResolvedProvider[] { + return resolveProviders(providers); } /** - * Resolves an array of bindings and creates an injector from those bindings. + * Resolves an array of providers and creates an injector from those providers. * - * The passed-in bindings can be an array of `Type`, {@link Binding}, - * or a recursive array of more bindings. + * The passed-in providers can be an array of `Type`, {@link Provider}, + * or a recursive array of more providers. * * ### Example ([live demo](http://plnkr.co/edit/ePOccA?p=preview)) * @@ -486,17 +485,17 @@ export class Injector { * expect(injector.get(Car) instanceof Car).toBe(true); * ``` * - * This function is slower than the corresponding `fromResolvedBindings` - * because it needs to resolve the passed-in bindings first. - * See {@link resolve} and {@link fromResolvedBindings}. + * This function is slower than the corresponding `fromResolvedProviders` + * because it needs to resolve the passed-in providers first. + * See {@link resolve} and {@link fromResolvedProviders}. */ - static resolveAndCreate(bindings: Array): Injector { - var resolvedBindings = Injector.resolve(bindings); - return Injector.fromResolvedBindings(resolvedBindings); + static resolveAndCreate(providers: Array): Injector { + var resolvedProviders = Injector.resolve(providers); + return Injector.fromResolvedProviders(resolvedProviders); } /** - * Creates an injector from previously resolved bindings. + * Creates an injector from previously resolved providers. * * This API is the recommended way to construct injectors in performance-sensitive parts. * @@ -512,17 +511,24 @@ export class Injector { * constructor(public engine:Engine) {} * } * - * var bindings = Injector.resolve([Car, Engine]); - * var injector = Injector.fromResolvedBindings(bindings); + * var providers = Injector.resolve([Car, Engine]); + * var injector = Injector.fromResolvedProviders(providers); * expect(injector.get(Car) instanceof Car).toBe(true); * ``` */ - static fromResolvedBindings(bindings: ResolvedBinding[]): Injector { - var bd = bindings.map(b => new BindingWithVisibility(b, Visibility.Public)); + static fromResolvedProviders(providers: ResolvedProvider[]): Injector { + var bd = providers.map(b => new ProviderWithVisibility(b, Visibility.Public)); var proto = new ProtoInjector(bd); return new Injector(proto, null, null); } + /** + * @deprecated + */ + static fromResolvedBindings(providers: ResolvedProvider[]): Injector { + return Injector.fromResolvedProviders(providers); + } + /** @internal */ _strategy: InjectorStrategy; /** @internal */ @@ -551,13 +557,13 @@ export class Injector { /** * Retrieves an instance from the injector based on the provided token. - * Throws {@link NoBindingError} if not found. + * Throws {@link NoProviderError} if not found. * * ### Example ([live demo](http://plnkr.co/edit/HeXSHg?p=preview)) * * ```typescript * var injector = Injector.resolveAndCreate([ - * bind("validToken").toValue("Value") + * provide("validToken", {asValue: "Value"}) * ]); * expect(injector.get("validToken")).toEqual("Value"); * expect(() => injector.get("invalidToken")).toThrowError(); @@ -582,7 +588,7 @@ export class Injector { * * ```typescript * var injector = Injector.resolveAndCreate([ - * bind("validToken").toValue("Value") + * provide("validToken", {asValue: "Value"}) * ]); * expect(injector.getOptional("validToken")).toEqual("Value"); * expect(injector.getOptional("invalidToken")).toBe(null); @@ -628,39 +634,39 @@ export class Injector { get internalStrategy(): any { return this._strategy; } /** - * Resolves an array of bindings and creates a child injector from those bindings. + * Resolves an array of providers and creates a child injector from those providers. * * * - * The passed-in bindings can be an array of `Type`, {@link Binding}, - * or a recursive array of more bindings. + * The passed-in providers can be an array of `Type`, {@link Provider}, + * or a recursive array of more providers. * * ### Example ([live demo](http://plnkr.co/edit/opB3T4?p=preview)) * * ```typescript - * class ParentBinding {} - * class ChildBinding {} + * class ParentProvider {} + * class ChildProvider {} * - * var parent = Injector.resolveAndCreate([ParentBinding]); - * var child = parent.resolveAndCreateChild([ChildBinding]); + * var parent = Injector.resolveAndCreate([ParentProvider]); + * var child = parent.resolveAndCreateChild([ChildProvider]); * - * expect(child.get(ParentBinding) instanceof ParentBinding).toBe(true); - * expect(child.get(ChildBinding) instanceof ChildBinding).toBe(true); - * expect(child.get(ParentBinding)).toBe(parent.get(ParentBinding)); + * expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true); + * expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true); + * expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider)); * ``` * * This function is slower than the corresponding `createChildFromResolved` - * because it needs to resolve the passed-in bindings first. + * because it needs to resolve the passed-in providers first. * See {@link resolve} and {@link createChildFromResolved}. */ - resolveAndCreateChild(bindings: Array): Injector { - var resolvedBindings = Injector.resolve(bindings); - return this.createChildFromResolved(resolvedBindings); + resolveAndCreateChild(providers: Array): Injector { + var resolvedProviders = Injector.resolve(providers); + return this.createChildFromResolved(resolvedProviders); } /** - * Creates a child injector from previously resolved bindings. + * Creates a child injector from previously resolved providers. * * @@ -670,22 +676,22 @@ export class Injector { * ### Example ([live demo](http://plnkr.co/edit/VhyfjN?p=preview)) * * ```typescript - * class ParentBinding {} - * class ChildBinding {} + * class ParentProvider {} + * class ChildProvider {} * - * var parentBindings = Injector.resolve([ParentBinding]); - * var childBindings = Injector.resolve([ChildBinding]); + * var parentProviders = Injector.resolve([ParentProvider]); + * var childProviders = Injector.resolve([ChildProvider]); * - * var parent = Injector.fromResolvedBindings(parentBindings); - * var child = parent.createChildFromResolved(childBindings); + * var parent = Injector.fromResolvedProviders(parentProviders); + * var child = parent.createChildFromResolved(childProviders); * - * expect(child.get(ParentBinding) instanceof ParentBinding).toBe(true); - * expect(child.get(ChildBinding) instanceof ChildBinding).toBe(true); - * expect(child.get(ParentBinding)).toBe(parent.get(ParentBinding)); + * expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true); + * expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true); + * expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider)); * ``` */ - createChildFromResolved(bindings: ResolvedBinding[]): Injector { - var bd = bindings.map(b => new BindingWithVisibility(b, Visibility.Public)); + createChildFromResolved(providers: ResolvedProvider[]): Injector { + var bd = providers.map(b => new ProviderWithVisibility(b, Visibility.Public)); var proto = new ProtoInjector(bd); var inj = new Injector(proto, null, null); inj._parent = this; @@ -693,7 +699,7 @@ export class Injector { } /** - * Resolves a binding and instantiates an object in the context of the injector. + * Resolves a provider and instantiates an object in the context of the injector. * * The created object does not get cached by the injector. * @@ -716,12 +722,12 @@ export class Injector { * expect(car).not.toBe(injector.resolveAndInstantiate(Car)); * ``` */ - resolveAndInstantiate(binding: Type | Binding): any { - return this.instantiateResolved(Injector.resolve([binding])[0]); + resolveAndInstantiate(provider: Type | Provider): any { + return this.instantiateResolved(Injector.resolve([provider])[0]); } /** - * Instantiates an object using a resolved binding in the context of the injector. + * Instantiates an object using a resolved provider in the context of the injector. * * The created object does not get cached by the injector. * @@ -738,37 +744,37 @@ export class Injector { * } * * var injector = Injector.resolveAndCreate([Engine]); - * var carBinding = Injector.resolve([Car])[0]; - * var car = injector.instantiateResolved(carBinding); + * var carProvider = Injector.resolve([Car])[0]; + * var car = injector.instantiateResolved(carProvider); * expect(car.engine).toBe(injector.get(Engine)); - * expect(car).not.toBe(injector.instantiateResolved(carBinding)); + * expect(car).not.toBe(injector.instantiateResolved(carProvider)); * ``` */ - instantiateResolved(binding: ResolvedBinding): any { - return this._instantiateBinding(binding, Visibility.PublicAndPrivate); + instantiateResolved(provider: ResolvedProvider): any { + return this._instantiateProvider(provider, Visibility.PublicAndPrivate); } /** @internal */ - _new(binding: ResolvedBinding, visibility: Visibility): any { + _new(provider: ResolvedProvider, visibility: Visibility): any { if (this._constructionCounter++ > this._strategy.getMaxNumberOfObjects()) { - throw new CyclicDependencyError(this, binding.key); + throw new CyclicDependencyError(this, provider.key); } - return this._instantiateBinding(binding, visibility); + return this._instantiateProvider(provider, visibility); } - private _instantiateBinding(binding: ResolvedBinding, visibility: Visibility): any { - if (binding.multiBinding) { - var res = ListWrapper.createFixedSize(binding.resolvedFactories.length); - for (var i = 0; i < binding.resolvedFactories.length; ++i) { - res[i] = this._instantiate(binding, binding.resolvedFactories[i], visibility); + private _instantiateProvider(provider: ResolvedProvider, visibility: Visibility): any { + if (provider.multiProvider) { + var res = ListWrapper.createFixedSize(provider.resolvedFactories.length); + for (var i = 0; i < provider.resolvedFactories.length; ++i) { + res[i] = this._instantiate(provider, provider.resolvedFactories[i], visibility); } return res; } else { - return this._instantiate(binding, binding.resolvedFactories[0], visibility); + return this._instantiate(provider, provider.resolvedFactories[0], visibility); } } - private _instantiate(binding: ResolvedBinding, resolvedFactory: ResolvedFactory, + private _instantiate(provider: ResolvedProvider, resolvedFactory: ResolvedFactory, visibility: Visibility): any { var factory = resolvedFactory.factory; var deps = resolvedFactory.dependencies; @@ -776,29 +782,29 @@ export class Injector { var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, d16, d17, d18, d19; try { - d0 = length > 0 ? this._getByDependency(binding, deps[0], visibility) : null; - d1 = length > 1 ? this._getByDependency(binding, deps[1], visibility) : null; - d2 = length > 2 ? this._getByDependency(binding, deps[2], visibility) : null; - d3 = length > 3 ? this._getByDependency(binding, deps[3], visibility) : null; - d4 = length > 4 ? this._getByDependency(binding, deps[4], visibility) : null; - d5 = length > 5 ? this._getByDependency(binding, deps[5], visibility) : null; - d6 = length > 6 ? this._getByDependency(binding, deps[6], visibility) : null; - d7 = length > 7 ? this._getByDependency(binding, deps[7], visibility) : null; - d8 = length > 8 ? this._getByDependency(binding, deps[8], visibility) : null; - d9 = length > 9 ? this._getByDependency(binding, deps[9], visibility) : null; - d10 = length > 10 ? this._getByDependency(binding, deps[10], visibility) : null; - d11 = length > 11 ? this._getByDependency(binding, deps[11], visibility) : null; - d12 = length > 12 ? this._getByDependency(binding, deps[12], visibility) : null; - d13 = length > 13 ? this._getByDependency(binding, deps[13], visibility) : null; - d14 = length > 14 ? this._getByDependency(binding, deps[14], visibility) : null; - d15 = length > 15 ? this._getByDependency(binding, deps[15], visibility) : null; - d16 = length > 16 ? this._getByDependency(binding, deps[16], visibility) : null; - d17 = length > 17 ? this._getByDependency(binding, deps[17], visibility) : null; - d18 = length > 18 ? this._getByDependency(binding, deps[18], visibility) : null; - d19 = length > 19 ? this._getByDependency(binding, deps[19], visibility) : null; + d0 = length > 0 ? this._getByDependency(provider, deps[0], visibility) : null; + d1 = length > 1 ? this._getByDependency(provider, deps[1], visibility) : null; + d2 = length > 2 ? this._getByDependency(provider, deps[2], visibility) : null; + d3 = length > 3 ? this._getByDependency(provider, deps[3], visibility) : null; + d4 = length > 4 ? this._getByDependency(provider, deps[4], visibility) : null; + d5 = length > 5 ? this._getByDependency(provider, deps[5], visibility) : null; + d6 = length > 6 ? this._getByDependency(provider, deps[6], visibility) : null; + d7 = length > 7 ? this._getByDependency(provider, deps[7], visibility) : null; + d8 = length > 8 ? this._getByDependency(provider, deps[8], visibility) : null; + d9 = length > 9 ? this._getByDependency(provider, deps[9], visibility) : null; + d10 = length > 10 ? this._getByDependency(provider, deps[10], visibility) : null; + d11 = length > 11 ? this._getByDependency(provider, deps[11], visibility) : null; + d12 = length > 12 ? this._getByDependency(provider, deps[12], visibility) : null; + d13 = length > 13 ? this._getByDependency(provider, deps[13], visibility) : null; + d14 = length > 14 ? this._getByDependency(provider, deps[14], visibility) : null; + d15 = length > 15 ? this._getByDependency(provider, deps[15], visibility) : null; + d16 = length > 16 ? this._getByDependency(provider, deps[16], visibility) : null; + d17 = length > 17 ? this._getByDependency(provider, deps[17], visibility) : null; + d18 = length > 18 ? this._getByDependency(provider, deps[18], visibility) : null; + d19 = length > 19 ? this._getByDependency(provider, deps[19], visibility) : null; } catch (e) { - if (e instanceof AbstractBindingError || e instanceof InstantiationError) { - e.addKey(this, binding.key); + if (e instanceof AbstractProviderError || e instanceof InstantiationError) { + e.addKey(this, provider.key); } throw e; } @@ -874,38 +880,38 @@ export class Injector { break; } } catch (e) { - throw new InstantiationError(this, e, e.stack, binding.key); + throw new InstantiationError(this, e, e.stack, provider.key); } return obj; } - private _getByDependency(binding: ResolvedBinding, dep: Dependency, - bindingVisibility: Visibility): any { + private _getByDependency(provider: ResolvedProvider, dep: Dependency, + providerVisibility: Visibility): any { var special = isPresent(this._depProvider) ? - this._depProvider.getDependency(this, binding, dep) : + this._depProvider.getDependency(this, provider, dep) : UNDEFINED; if (special !== UNDEFINED) { return special; } else { return this._getByKey(dep.key, dep.lowerBoundVisibility, dep.upperBoundVisibility, - dep.optional, bindingVisibility); + dep.optional, providerVisibility); } } private _getByKey(key: Key, lowerBoundVisibility: Object, upperBoundVisibility: Object, - optional: boolean, bindingVisibility: Visibility): any { + optional: boolean, providerVisibility: Visibility): any { if (key === INJECTOR_KEY) { return this; } if (upperBoundVisibility instanceof SelfMetadata) { - return this._getByKeySelf(key, optional, bindingVisibility); + return this._getByKeySelf(key, optional, providerVisibility); } else if (upperBoundVisibility instanceof HostMetadata) { - return this._getByKeyHost(key, optional, bindingVisibility, lowerBoundVisibility); + return this._getByKeyHost(key, optional, providerVisibility, lowerBoundVisibility); } else { - return this._getByKeyDefault(key, optional, bindingVisibility, lowerBoundVisibility); + return this._getByKeyDefault(key, optional, providerVisibility, lowerBoundVisibility); } } @@ -914,18 +920,18 @@ export class Injector { if (optional) { return null; } else { - throw new NoBindingError(this, key); + throw new NoProviderError(this, key); } } /** @internal */ - _getByKeySelf(key: Key, optional: boolean, bindingVisibility: Visibility): any { - var obj = this._strategy.getObjByKeyId(key.id, bindingVisibility); + _getByKeySelf(key: Key, optional: boolean, providerVisibility: Visibility): any { + var obj = this._strategy.getObjByKeyId(key.id, providerVisibility); return (obj !== UNDEFINED) ? obj : this._throwOrNull(key, optional); } /** @internal */ - _getByKeyHost(key: Key, optional: boolean, bindingVisibility: Visibility, + _getByKeyHost(key: Key, optional: boolean, providerVisibility: Visibility, lowerBoundVisibility: Object): any { var inj = this; @@ -938,7 +944,7 @@ export class Injector { } while (inj != null) { - var obj = inj._strategy.getObjByKeyId(key.id, bindingVisibility); + var obj = inj._strategy.getObjByKeyId(key.id, providerVisibility); if (obj !== UNDEFINED) return obj; if (isPresent(inj._parent) && inj._isHost) { @@ -958,20 +964,20 @@ export class Injector { } /** @internal */ - _getByKeyDefault(key: Key, optional: boolean, bindingVisibility: Visibility, + _getByKeyDefault(key: Key, optional: boolean, providerVisibility: Visibility, lowerBoundVisibility: Object): any { var inj = this; if (lowerBoundVisibility instanceof SkipSelfMetadata) { - bindingVisibility = inj._isHost ? Visibility.PublicAndPrivate : Visibility.Public; + providerVisibility = inj._isHost ? Visibility.PublicAndPrivate : Visibility.Public; inj = inj._parent; } while (inj != null) { - var obj = inj._strategy.getObjByKeyId(key.id, bindingVisibility); + var obj = inj._strategy.getObjByKeyId(key.id, providerVisibility); if (obj !== UNDEFINED) return obj; - bindingVisibility = inj._isHost ? Visibility.PublicAndPrivate : Visibility.Public; + providerVisibility = inj._isHost ? Visibility.PublicAndPrivate : Visibility.Public; inj = inj._parent; } @@ -979,7 +985,7 @@ export class Injector { } get displayName(): string { - return `Injector(bindings: [${_mapBindings(this, b => ` "${b.key.displayName}" `).join(", ")}])`; + return `Injector(providers: [${_mapProviders(this, b => ` "${b.key.displayName}" `).join(", ")}])`; } toString(): string { return this.displayName; } @@ -988,10 +994,10 @@ export class Injector { var INJECTOR_KEY = Key.get(Injector); -function _mapBindings(injector: Injector, fn: Function): any[] { +function _mapProviders(injector: Injector, fn: Function): any[] { var res = []; - for (var i = 0; i < injector._proto.numberOfBindings; ++i) { - res.push(fn(injector._proto.getBindingAtIndex(i))); + for (var i = 0; i < injector._proto.numberOfProviders; ++i) { + res.push(fn(injector._proto.getProviderAtIndex(i))); } return res; } diff --git a/modules/angular2/src/core/di/key.ts b/modules/angular2/src/core/di/key.ts index 0f99d12125..5518a2ac32 100644 --- a/modules/angular2/src/core/di/key.ts +++ b/modules/angular2/src/core/di/key.ts @@ -16,7 +16,7 @@ export {TypeLiteral} from './type_literal'; * injector to store created objects in a more efficient way. * * `Key` should not be created directly. {@link Injector} creates keys automatically when resolving - * bindings. + * providers. */ export class Key { /** diff --git a/modules/angular2/src/core/di/metadata.ts b/modules/angular2/src/core/di/metadata.ts index bb3a7742b3..bda69c3dd7 100644 --- a/modules/angular2/src/core/di/metadata.ts +++ b/modules/angular2/src/core/di/metadata.ts @@ -17,7 +17,7 @@ import {CONST, CONST_EXPR, stringify, isBlank, isPresent} from "angular2/src/cor * } * * var injector = Injector.resolveAndCreate([ - * bind("MyEngine").toClass(Engine), + * provide("MyEngine", {asClass: Engine}), * Car * ]); * @@ -212,7 +212,7 @@ export class SkipSelfMetadata { * * @Component({ * selector: 'parent-cmp', - * bindings: [HostService] + * providers: [HostService] * }) * @View({ * template: ` @@ -225,7 +225,7 @@ export class SkipSelfMetadata { * * @Component({ * selector: 'app', - * bindings: [OtherService] + * providers: [OtherService] * }) * @View({ * template: ` diff --git a/modules/angular2/src/core/di/opaque_token.ts b/modules/angular2/src/core/di/opaque_token.ts index 6d65da122d..e783b6329b 100644 --- a/modules/angular2/src/core/di/opaque_token.ts +++ b/modules/angular2/src/core/di/opaque_token.ts @@ -1,22 +1,22 @@ import {CONST} from 'angular2/src/core/facade/lang'; /** - * Creates a token that can be used in a DI Binding. + * Creates a token that can be used in a DI Provider. * * ### Example ([live demo](http://plnkr.co/edit/Ys9ezXpj2Mnoy3Uc8KBp?p=preview)) * * ```typescript - * var t = new OpaqueToken("binding"); + * var t = new OpaqueToken("value"); * * var injector = Injector.resolveAndCreate([ - * bind(t).toValue("bindingValue") + * provide(t, {asValue: "providedValue"}) * ]); * * expect(injector.get(t)).toEqual("bindingValue"); * ``` * * Using an `OpaqueToken` is preferable to using strings as tokens because of possible collisions - * caused by multiple bindings using the same string as two different tokens. + * caused by multiple providers using the same string as two different tokens. * * Using an `OpaqueToken` is preferable to using an `Object` as tokens because it provides better * error messages. diff --git a/modules/angular2/src/core/di/binding.ts b/modules/angular2/src/core/di/provider.ts similarity index 63% rename from modules/angular2/src/core/di/binding.ts rename to modules/angular2/src/core/di/provider.ts index ad44cd5d20..a5d8389f11 100644 --- a/modules/angular2/src/core/di/binding.ts +++ b/modules/angular2/src/core/di/provider.ts @@ -25,8 +25,8 @@ import { } from './metadata'; import { NoAnnotationError, - MixingMultiBindingsWithRegularBindings, - InvalidBindingError + MixingMultiProvidersWithRegularProvidersError, + InvalidProviderError } from './exceptions'; import {resolveForwardRef} from './forward_ref'; @@ -42,22 +42,22 @@ const _EMPTY_LIST = CONST_EXPR([]); /** * Describes how the {@link Injector} should instantiate a given token. * - * See {@link bind}. + * See {@link provide}. * * ### Example ([live demo](http://plnkr.co/edit/GNAyj6K6PfYg2NBzgwZ5?p%3Dpreview&p=preview)) * * ```javascript * var injector = Injector.resolveAndCreate([ - * new Binding("message", { toValue: 'Hello' }) + * new Provider("message", { toValue: 'Hello' }) * ]); * * expect(injector.get("message")).toEqual('Hello'); * ``` */ @CONST() -export class Binding { +export class Provider { /** - * Token used when retrieving this binding. Usually, it is a type {@link `Type`}. + * Token used when retrieving this provider. Usually, it is a type {@link `Type`}. */ token; @@ -77,11 +77,11 @@ export class Binding { * * var injectorClass = Injector.resolveAndCreate([ * Car, - * new Binding(Vehicle, { toClass: Car }) + * new Provider(Vehicle, { toClass: Car }) * ]); * var injectorAlias = Injector.resolveAndCreate([ * Car, - * new Binding(Vehicle, { toAlias: Car }) + * new Provider(Vehicle, { toAlias: Car }) * ]); * * expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car)); @@ -100,7 +100,7 @@ export class Binding { * * ```javascript * var injector = Injector.resolveAndCreate([ - * new Binding("message", { toValue: 'Hello' }) + * new Provider("message", { toValue: 'Hello' }) * ]); * * expect(injector.get("message")).toEqual('Hello'); @@ -126,11 +126,11 @@ export class Binding { * * var injectorAlias = Injector.resolveAndCreate([ * Car, - * new Binding(Vehicle, { toAlias: Car }) + * new Provider(Vehicle, { toAlias: Car }) * ]); * var injectorClass = Injector.resolveAndCreate([ * Car, - * new Binding(Vehicle, { toClass: Car }) + * new Provider(Vehicle, { toClass: Car }) * ]); * * expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car)); @@ -149,8 +149,8 @@ export class Binding { * * ```typescript * var injector = Injector.resolveAndCreate([ - * new Binding(Number, { toFactory: () => { return 1+2; }}), - * new Binding(String, { toFactory: (value) => { return "Value: " + value; }, + * new Provider(Number, { toFactory: () => { return 1+2; }}), + * new Provider(String, { toFactory: (value) => { return "Value: " + value; }, * deps: [Number] }) * ]); * @@ -170,8 +170,8 @@ export class Binding { * * ```typescript * var injector = Injector.resolveAndCreate([ - * new Binding(Number, { toFactory: () => { return 1+2; }}), - * new Binding(String, { toFactory: (value) => { return "Value: " + value; }, + * new Provider(Number, { toFactory: () => { return 1+2; }}), + * new Provider(String, { toFactory: (value) => { return "Value: " + value; }, * deps: [Number] }) * ]); * @@ -205,40 +205,63 @@ export class Binding { // TODO: Provide a full working example after alpha38 is released. /** - * Creates multiple bindings matching the same token (a multi-binding). + * Creates multiple providers matching the same token (a multi-provider). * - * Multi-bindings are used for creating pluggable service, where the system comes - * with some default bindings, and the user can register additonal bindings. - * The combination of the default bindings and the additional bindings will be + * Multi-providers are used for creating pluggable service, where the system comes + * with some default providers, and the user can register additonal providers. + * The combination of the default providers and the additional providers will be * used to drive the behavior of the system. * * ### Example * * ```typescript * var injector = Injector.resolveAndCreate([ - * new Binding("Strings", { toValue: "String1", multi: true}), - * new Binding("Strings", { toValue: "String2", multi: true}) + * new Provider("Strings", { toValue: "String1", multi: true}), + * new Provider("Strings", { toValue: "String2", multi: true}) * ]); * * expect(injector.get("Strings")).toEqual(["String1", "String2"]); * ``` * - * Multi-bindings and regular bindings cannot be mixed. The following + * Multi-providers and regular providers cannot be mixed. The following * will throw an exception: * * ```typescript * var injector = Injector.resolveAndCreate([ - * new Binding("Strings", { toValue: "String1", multi: true }), - * new Binding("Strings", { toValue: "String2"}) + * new Provider("Strings", { toValue: "String1", multi: true }), + * new Provider("Strings", { toValue: "String2"}) * ]); * ``` */ get multi(): boolean { return normalizeBool(this._multi); } } +/** + * @deprecated + */ +@CONST() +export class Binding extends Provider { + constructor(token, {toClass, toValue, toAlias, toFactory, deps, multi}: { + toClass?: Type, + toValue?: any, + toAlias?: any, + toFactory?: Function, + deps?: Object[], + multi?: boolean + }) { + super(token, { + toClass: toClass, + toValue: toValue, + toAlias: toAlias, + toFactory: toFactory, + deps: deps, + multi: multi + }); + } +} /** - * An internal resolved representation of a {@link Binding} used by the {@link Injector}. + * An internal resolved representation of a {@link Provider} used by the {@link Injector}. * * It is usually created automatically by `Injector.resolveAndCreate`. * @@ -247,13 +270,13 @@ export class Binding { * ### Example ([live demo](http://plnkr.co/edit/RfEnhh8kUEI0G3qsnIeT?p%3Dpreview&p=preview)) * * ```typescript - * var resolvedBindings = Injector.resolve([new Binding('message', {toValue: 'Hello'})]); - * var injector = Injector.fromResolvedBindings(resolvedBindings); + * var resolvedProviders = Injector.resolve([new Provider('message', {toValue: 'Hello'})]); + * var injector = Injector.fromResolvedProviders(resolvedProviders); * * expect(injector.get('message')).toEqual('Hello'); * ``` */ -export interface ResolvedBinding { +export interface ResolvedProvider { /** * A key, usually a `Type`. */ @@ -265,20 +288,25 @@ export interface ResolvedBinding { resolvedFactories: ResolvedFactory[]; /** - * Indicates if the binding is a multi-binding or a regular binding. + * Indicates if the provider is a multi-provider or a regular provider. */ - multiBinding: boolean; + multiProvider: boolean; } -export class ResolvedBinding_ implements ResolvedBinding { +/** + * @deprecated + */ +export interface ResolvedBinding extends ResolvedProvider {} + +export class ResolvedProvider_ implements ResolvedBinding { constructor(public key: Key, public resolvedFactories: ResolvedFactory[], - public multiBinding: boolean) {} + public multiProvider: boolean) {} get resolvedFactory(): ResolvedFactory { return this.resolvedFactories[0]; } } /** - * An internal resolved representation of a factory function created by resolving {@link Binding}. + * An internal resolved representation of a factory function created by resolving {@link Provider}. */ export class ResolvedFactory { constructor( @@ -294,22 +322,49 @@ export class ResolvedFactory { } /** - * Creates a {@link Binding}. + * @deprecated + * Creates a {@link Provider}. * - * To construct a {@link Binding}, bind a `token` to either a class, a value, a factory function, or + * To construct a {@link Provider}, bind a `token` to either a class, a value, a factory function, + * or * to an alias to another `token`. - * See {@link BindingBuilder} for more details. + * See {@link ProviderBuilder} for more details. * * The `token` is most commonly a class or {@link angular2/di/OpaqueToken}. */ -export function bind(token): BindingBuilder { - return new BindingBuilder(token); +export function bind(token): ProviderBuilder { + return new ProviderBuilder(token); +} + +/** + * Creates a {@link Provider}. + * + * See {@link Provider} for more details. + * + * + */ +export function provide(token, {asClass, asValue, asAlias, asFactory, deps, multi}: { + asClass?: Type, + asValue?: any, + asAlias?: any, + asFactory?: Function, + deps?: Object[], + multi?: boolean +}): Provider { + return new Provider(token, { + toClass: asClass, + toValue: asValue, + toAlias: asAlias, + toFactory: asFactory, + deps: deps, + multi: multi + }); } /** * Helper class for the {@link bind} function. */ -export class BindingBuilder { +export class ProviderBuilder { constructor(public token) {} /** @@ -327,11 +382,11 @@ export class BindingBuilder { * * var injectorClass = Injector.resolveAndCreate([ * Car, - * bind(Vehicle).toClass(Car) + * provide(Vehicle, {asClass: Car}) * ]); * var injectorAlias = Injector.resolveAndCreate([ * Car, - * bind(Vehicle).toAlias(Car) + * provide(Vehicle, {asAlias: Car}) * ]); * * expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car)); @@ -341,12 +396,12 @@ export class BindingBuilder { * expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true); * ``` */ - toClass(type: Type): Binding { + toClass(type: Type): Provider { if (!isType(type)) { throw new BaseException( - `Trying to create a class binding but "${stringify(type)}" is not a class!`); + `Trying to create a class provider but "${stringify(type)}" is not a class!`); } - return new Binding(this.token, {toClass: type}); + return new Provider(this.token, {toClass: type}); } /** @@ -356,13 +411,13 @@ export class BindingBuilder { * * ```typescript * var injector = Injector.resolveAndCreate([ - * bind('message').toValue('Hello') + * provide('message', {asValue: 'Hello'}) * ]); * * expect(injector.get('message')).toEqual('Hello'); * ``` */ - toValue(value: any): Binding { return new Binding(this.token, {toValue: value}); } + toValue(value: any): Provider { return new Provider(this.token, {toValue: value}); } /** * Binds a DI token as an alias for an existing token. @@ -383,11 +438,11 @@ export class BindingBuilder { * * var injectorAlias = Injector.resolveAndCreate([ * Car, - * bind(Vehicle).toAlias(Car) + * provide(Vehicle, {asAlias: Car}) * ]); * var injectorClass = Injector.resolveAndCreate([ * Car, - * bind(Vehicle).toClass(Car) + * provide(Vehicle, {asClass: Car}) * ]); * * expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car)); @@ -397,11 +452,11 @@ export class BindingBuilder { * expect(injectorClass.get(Vehicle) instanceof Car).toBe(true); * ``` */ - toAlias(aliasToken: /*Type*/ any): Binding { + toAlias(aliasToken: /*Type*/ any): Provider { if (isBlank(aliasToken)) { throw new BaseException(`Can not alias ${stringify(this.token)} to a blank value!`); } - return new Binding(this.token, {toAlias: aliasToken}); + return new Provider(this.token, {toAlias: aliasToken}); } /** @@ -411,69 +466,69 @@ export class BindingBuilder { * * ```typescript * var injector = Injector.resolveAndCreate([ - * bind(Number).toFactory(() => { return 1+2; }), - * bind(String).toFactory((v) => { return "Value: " + v; }, [Number]) + * provide(Number, {asFactory: () => { return 1+2; }}), + * provide(String, {asFactory: (v) => { return "Value: " + v; }, deps: [Number]}) * ]); * * expect(injector.get(Number)).toEqual(3); * expect(injector.get(String)).toEqual('Value: 3'); * ``` */ - toFactory(factory: Function, dependencies?: any[]): Binding { + toFactory(factory: Function, dependencies?: any[]): Provider { if (!isFunction(factory)) { throw new BaseException( - `Trying to create a factory binding but "${stringify(factory)}" is not a function!`); + `Trying to create a factory provider but "${stringify(factory)}" is not a function!`); } - return new Binding(this.token, {toFactory: factory, deps: dependencies}); + return new Provider(this.token, {toFactory: factory, deps: dependencies}); } } /** - * Resolve a single binding. + * Resolve a single provider. */ -export function resolveFactory(binding: Binding): ResolvedFactory { +export function resolveFactory(provider: Provider): ResolvedFactory { var factoryFn: Function; var resolvedDeps; - if (isPresent(binding.toClass)) { - var toClass = resolveForwardRef(binding.toClass); + if (isPresent(provider.toClass)) { + var toClass = resolveForwardRef(provider.toClass); factoryFn = reflector.factory(toClass); resolvedDeps = _dependenciesFor(toClass); - } else if (isPresent(binding.toAlias)) { + } else if (isPresent(provider.toAlias)) { factoryFn = (aliasInstance) => aliasInstance; - resolvedDeps = [Dependency.fromKey(Key.get(binding.toAlias))]; - } else if (isPresent(binding.toFactory)) { - factoryFn = binding.toFactory; - resolvedDeps = _constructDependencies(binding.toFactory, binding.dependencies); + resolvedDeps = [Dependency.fromKey(Key.get(provider.toAlias))]; + } else if (isPresent(provider.toFactory)) { + factoryFn = provider.toFactory; + resolvedDeps = _constructDependencies(provider.toFactory, provider.dependencies); } else { - factoryFn = () => binding.toValue; + factoryFn = () => provider.toValue; resolvedDeps = _EMPTY_LIST; } return new ResolvedFactory(factoryFn, resolvedDeps); } /** - * Converts the {@link Binding} into {@link ResolvedBinding}. + * Converts the {@link Provider} into {@link ResolvedProvider}. * - * {@link Injector} internally only uses {@link ResolvedBinding}, {@link Binding} contains - * convenience binding syntax. + * {@link Injector} internally only uses {@link ResolvedProvider}, {@link Provider} contains + * convenience provider syntax. */ -export function resolveBinding(binding: Binding): ResolvedBinding { - return new ResolvedBinding_(Key.get(binding.token), [resolveFactory(binding)], false); +export function resolveProvider(provider: Provider): ResolvedProvider { + return new ResolvedProvider_(Key.get(provider.token), [resolveFactory(provider)], false); } /** - * Resolve a list of Bindings. + * Resolve a list of Providers. */ -export function resolveBindings(bindings: Array): ResolvedBinding[] { - var normalized = _createListOfBindings( - _normalizeBindings(bindings, new Map())); +export function resolveProviders(providers: Array): ResolvedProvider[] { + var normalized = _createListOfProviders(_normalizeProviders( + providers, new Map())); return normalized.map(b => { - if (b instanceof _NormalizedBinding) { - return new ResolvedBinding_(b.key, [b.resolvedFactory], false); + if (b instanceof _NormalizedProvider) { + return new ResolvedProvider_(b.key, [b.resolvedFactory], false); } else { - var arr = <_NormalizedBinding[]>b; - return new ResolvedBinding_(arr[0].key, arr.map(_ => _.resolvedFactory), true); + var arr = <_NormalizedProvider[]>b; + return new ResolvedProvider_(arr[0].key, arr.map(_ => _.resolvedFactory), true); } }); } @@ -481,66 +536,66 @@ export function resolveBindings(bindings: Array): Resolv /** * The algorithm works as follows: * - * [Binding] -> [_NormalizedBinding|[_NormalizedBinding]] -> [ResolvedBinding] + * [Provider] -> [_NormalizedProvider|[_NormalizedProvider]] -> [ResolvedProvider] * - * _NormalizedBinding is essentially a resolved binding before it was grouped by key. + * _NormalizedProvider is essentially a resolved provider before it was grouped by key. */ -class _NormalizedBinding { +class _NormalizedProvider { constructor(public key: Key, public resolvedFactory: ResolvedFactory) {} } -function _createListOfBindings(flattenedBindings: Map): any[] { - return MapWrapper.values(flattenedBindings); +function _createListOfProviders(flattenedProviders: Map): any[] { + return MapWrapper.values(flattenedProviders); } -function _normalizeBindings(bindings: Array, - res: Map): - Map { - bindings.forEach(b => { +function _normalizeProviders(providers: Array, + res: Map): + Map { + providers.forEach(b => { if (b instanceof Type) { - _normalizeBinding(bind(b).toClass(b), res); + _normalizeProvider(provide(b, {asClass: b}), res); - } else if (b instanceof Binding) { - _normalizeBinding(b, res); + } else if (b instanceof Provider) { + _normalizeProvider(b, res); } else if (b instanceof Array) { - _normalizeBindings(b, res); + _normalizeProviders(b, res); - } else if (b instanceof BindingBuilder) { - throw new InvalidBindingError(b.token); + } else if (b instanceof ProviderBuilder) { + throw new InvalidProviderError(b.token); } else { - throw new InvalidBindingError(b); + throw new InvalidProviderError(b); } }); return res; } -function _normalizeBinding(b: Binding, res: Map): - void { +function _normalizeProvider(b: Provider, + res: Map): void { var key = Key.get(b.token); var factory = resolveFactory(b); - var normalized = new _NormalizedBinding(key, factory); + var normalized = new _NormalizedProvider(key, factory); if (b.multi) { - var existingBinding = res.get(key.id); + var existingProvider = res.get(key.id); - if (existingBinding instanceof Array) { - existingBinding.push(normalized); + if (existingProvider instanceof Array) { + existingProvider.push(normalized); - } else if (isBlank(existingBinding)) { + } else if (isBlank(existingProvider)) { res.set(key.id, [normalized]); } else { - throw new MixingMultiBindingsWithRegularBindings(existingBinding, b); + throw new MixingMultiProvidersWithRegularProvidersError(existingProvider, b); } } else { - var existingBinding = res.get(key.id); + var existingProvider = res.get(key.id); - if (existingBinding instanceof Array) { - throw new MixingMultiBindingsWithRegularBindings(existingBinding, b); + if (existingProvider instanceof Array) { + throw new MixingMultiProvidersWithRegularProvidersError(existingProvider, b); } res.set(key.id, normalized); diff --git a/modules/angular2/src/core/facade/exception_handler.ts b/modules/angular2/src/core/facade/exception_handler.ts index 0a9b254a06..8410abb803 100644 --- a/modules/angular2/src/core/facade/exception_handler.ts +++ b/modules/angular2/src/core/facade/exception_handler.ts @@ -27,7 +27,7 @@ class _ArrayLogger { * } * } * - * bootstrap(MyApp, [bind(ExceptionHandler).toClass(MyExceptionHandler)]) + * bootstrap(MyApp, [provide(ExceptionHandler, {asClass: MyExceptionHandler})]) * * ``` */ diff --git a/modules/angular2/src/core/forms.ts b/modules/angular2/src/core/forms.ts index bd156d46d5..31201aa37d 100644 --- a/modules/angular2/src/core/forms.ts +++ b/modules/angular2/src/core/forms.ts @@ -41,12 +41,17 @@ import {FormBuilder} from './forms/form_builder'; import {CONST_EXPR, Type} from './facade/lang'; /** - * Shorthand set of bindings used for building Angular forms. + * Shorthand set of providers used for building Angular forms. * * ### Example: * * ```typescript - * bootstrap(MyApp, [FORM_BINDINGS]); + * bootstrap(MyApp, [FORM_PROVIDERS]); * ``` */ -export const FORM_BINDINGS: Type[] = CONST_EXPR([FormBuilder]); +export const FORM_PROVIDERS: Type[] = CONST_EXPR([FormBuilder]); + +/** + * @deprecated + */ +export const FORM_BINDINGS = FORM_PROVIDERS; diff --git a/modules/angular2/src/core/forms/directives/checkbox_value_accessor.ts b/modules/angular2/src/core/forms/directives/checkbox_value_accessor.ts index 89d4a06925..b01c5dcecb 100644 --- a/modules/angular2/src/core/forms/directives/checkbox_value_accessor.ts +++ b/modules/angular2/src/core/forms/directives/checkbox_value_accessor.ts @@ -1,13 +1,13 @@ import {Directive} from 'angular2/src/core/metadata'; import {Renderer} from 'angular2/src/core/render'; import {ElementRef} from 'angular2/src/core/linker'; -import {Self, forwardRef, Binding} from 'angular2/src/core/di'; +import {Self, forwardRef, Provider} from 'angular2/src/core/di'; import {NG_VALUE_ACCESSOR, ControlValueAccessor} from './control_value_accessor'; import {CONST_EXPR} from 'angular2/src/core/facade/lang'; import {setProperty} from './shared'; -const CHECKBOX_VALUE_ACCESSOR = CONST_EXPR(new Binding( +const CHECKBOX_VALUE_ACCESSOR = CONST_EXPR(new Provider( NG_VALUE_ACCESSOR, {toAlias: forwardRef(() => CheckboxControlValueAccessor), multi: true})); /** diff --git a/modules/angular2/src/core/forms/directives/default_value_accessor.ts b/modules/angular2/src/core/forms/directives/default_value_accessor.ts index 72b729ca5d..e0c8030a44 100644 --- a/modules/angular2/src/core/forms/directives/default_value_accessor.ts +++ b/modules/angular2/src/core/forms/directives/default_value_accessor.ts @@ -1,13 +1,13 @@ import {Directive} from 'angular2/src/core/metadata'; import {ElementRef} from 'angular2/src/core/linker'; import {Renderer} from 'angular2/src/core/render'; -import {Self, forwardRef, Binding} from 'angular2/src/core/di'; +import {Self, forwardRef, Provider} from 'angular2/src/core/di'; import {NG_VALUE_ACCESSOR, ControlValueAccessor} from './control_value_accessor'; import {isBlank, CONST_EXPR} from 'angular2/src/core/facade/lang'; import {setProperty} from './shared'; -const DEFAULT_VALUE_ACCESSOR = CONST_EXPR( - new Binding(NG_VALUE_ACCESSOR, {toAlias: forwardRef(() => DefaultValueAccessor), multi: true})); +const DEFAULT_VALUE_ACCESSOR = CONST_EXPR(new Provider( + NG_VALUE_ACCESSOR, {toAlias: forwardRef(() => DefaultValueAccessor), multi: true})); /** * The default accessor for writing a value and listening to changes that is used by the diff --git a/modules/angular2/src/core/forms/directives/ng_control_group.ts b/modules/angular2/src/core/forms/directives/ng_control_group.ts index 70eb1e6b3f..6b9fefce43 100644 --- a/modules/angular2/src/core/forms/directives/ng_control_group.ts +++ b/modules/angular2/src/core/forms/directives/ng_control_group.ts @@ -1,6 +1,6 @@ import {OnInit, OnDestroy} from 'angular2/lifecycle_hooks'; import {Directive} from 'angular2/src/core/metadata'; -import {Inject, Host, SkipSelf, forwardRef, Binding} from 'angular2/src/core/di'; +import {Inject, Host, SkipSelf, forwardRef, Provider} from 'angular2/src/core/di'; import {ListWrapper} from 'angular2/src/core/facade/collection'; import {CONST_EXPR} from 'angular2/src/core/facade/lang'; @@ -10,7 +10,7 @@ import {ControlGroup} from '../model'; import {Form} from './form_interface'; const controlGroupBinding = - CONST_EXPR(new Binding(ControlContainer, {toAlias: forwardRef(() => NgControlGroup)})); + CONST_EXPR(new Provider(ControlContainer, {toAlias: forwardRef(() => NgControlGroup)})); /** * Creates and binds a control group to a DOM element. diff --git a/modules/angular2/src/core/forms/directives/ng_control_name.ts b/modules/angular2/src/core/forms/directives/ng_control_name.ts index 3578776827..87ab279eb2 100644 --- a/modules/angular2/src/core/forms/directives/ng_control_name.ts +++ b/modules/angular2/src/core/forms/directives/ng_control_name.ts @@ -3,7 +3,7 @@ import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async'; import {OnChanges, OnDestroy} from 'angular2/lifecycle_hooks'; import {SimpleChange} from 'angular2/src/core/change_detection'; import {Query, Directive} from 'angular2/src/core/metadata'; -import {forwardRef, Host, SkipSelf, Binding, Inject, Optional} from 'angular2/src/core/di'; +import {forwardRef, Host, SkipSelf, Provider, Inject, Optional} from 'angular2/src/core/di'; import {ControlContainer} from './control_container'; import {NgControl} from './ng_control'; @@ -14,7 +14,7 @@ import {Validators, NG_VALIDATORS} from '../validators'; const controlNameBinding = - CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgControlName)})); + CONST_EXPR(new Provider(NgControl, {toAlias: forwardRef(() => NgControlName)})); /** * Creates and binds a control with a specified name to a DOM element. diff --git a/modules/angular2/src/core/forms/directives/ng_form.ts b/modules/angular2/src/core/forms/directives/ng_form.ts index e7d3381d12..15bcded85f 100644 --- a/modules/angular2/src/core/forms/directives/ng_form.ts +++ b/modules/angular2/src/core/forms/directives/ng_form.ts @@ -7,7 +7,7 @@ import { import {StringMapWrapper, ListWrapper} from 'angular2/src/core/facade/collection'; import {isPresent, isBlank, CONST_EXPR} from 'angular2/src/core/facade/lang'; import {Directive} from 'angular2/src/core/metadata'; -import {forwardRef, Binding} from 'angular2/src/core/di'; +import {forwardRef, Provider} from 'angular2/src/core/di'; import {NgControl} from './ng_control'; import {Form} from './form_interface'; import {NgControlGroup} from './ng_control_group'; @@ -15,8 +15,8 @@ import {ControlContainer} from './control_container'; import {AbstractControl, ControlGroup, Control} from '../model'; import {setUpControl} from './shared'; -const formDirectiveBinding = - CONST_EXPR(new Binding(ControlContainer, {toAlias: forwardRef(() => NgForm)})); +const formDirectiveProvider = + CONST_EXPR(new Provider(ControlContainer, {toAlias: forwardRef(() => NgForm)})); /** * If `NgForm` is bound in a component, `
` elements in that component will be @@ -81,7 +81,7 @@ const formDirectiveBinding = */ @Directive({ selector: 'form:not([ng-no-form]):not([ng-form-model]),ng-form,[ng-form]', - bindings: [formDirectiveBinding], + bindings: [formDirectiveProvider], host: { '(submit)': 'onSubmit()', }, diff --git a/modules/angular2/src/core/forms/directives/ng_form_control.ts b/modules/angular2/src/core/forms/directives/ng_form_control.ts index a896d2b410..cbf982dd34 100644 --- a/modules/angular2/src/core/forms/directives/ng_form_control.ts +++ b/modules/angular2/src/core/forms/directives/ng_form_control.ts @@ -3,7 +3,7 @@ import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async'; import {OnChanges} from 'angular2/lifecycle_hooks'; import {SimpleChange} from 'angular2/src/core/change_detection'; import {Query, Directive} from 'angular2/src/core/metadata'; -import {forwardRef, Binding, Inject, Optional} from 'angular2/src/core/di'; +import {forwardRef, Provider, Inject, Optional} from 'angular2/src/core/di'; import {NgControl} from './ng_control'; import {Control} from '../model'; import {Validators, NG_VALIDATORS} from '../validators'; @@ -11,7 +11,7 @@ import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor' import {setUpControl, isPropertyUpdated, selectValueAccessor} from './shared'; const formControlBinding = - CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgFormControl)})); + CONST_EXPR(new Provider(NgControl, {toAlias: forwardRef(() => NgFormControl)})); /** * Binds an existing {@link Control} to a DOM element. diff --git a/modules/angular2/src/core/forms/directives/ng_form_model.ts b/modules/angular2/src/core/forms/directives/ng_form_model.ts index 5dcd78d9c7..ab48f914b8 100644 --- a/modules/angular2/src/core/forms/directives/ng_form_model.ts +++ b/modules/angular2/src/core/forms/directives/ng_form_model.ts @@ -4,7 +4,7 @@ import {ObservableWrapper, EventEmitter} from 'angular2/src/core/facade/async'; import {OnChanges} from 'angular2/lifecycle_hooks'; import {Directive} from 'angular2/src/core/metadata'; -import {forwardRef, Binding} from 'angular2/src/core/di'; +import {forwardRef, Provider} from 'angular2/src/core/di'; import {NgControl} from './ng_control'; import {NgControlGroup} from './ng_control_group'; import {ControlContainer} from './control_container'; @@ -12,8 +12,8 @@ import {Form} from './form_interface'; import {Control, ControlGroup} from '../model'; import {setUpControl} from './shared'; -const formDirectiveBinding = - CONST_EXPR(new Binding(ControlContainer, {toAlias: forwardRef(() => NgFormModel)})); +const formDirectiveProvider = + CONST_EXPR(new Provider(ControlContainer, {toAlias: forwardRef(() => NgFormModel)})); /** * Binds an existing control group to a DOM element. @@ -91,7 +91,7 @@ const formDirectiveBinding = */ @Directive({ selector: '[ng-form-model]', - bindings: [formDirectiveBinding], + bindings: [formDirectiveProvider], inputs: ['form: ng-form-model'], host: {'(submit)': 'onSubmit()'}, outputs: ['ngSubmit'], diff --git a/modules/angular2/src/core/forms/directives/ng_model.ts b/modules/angular2/src/core/forms/directives/ng_model.ts index 427c34afc2..e1eb6ecee2 100644 --- a/modules/angular2/src/core/forms/directives/ng_model.ts +++ b/modules/angular2/src/core/forms/directives/ng_model.ts @@ -3,14 +3,15 @@ import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async'; import {OnChanges} from 'angular2/lifecycle_hooks'; import {SimpleChange} from 'angular2/src/core/change_detection'; import {Query, Directive} from 'angular2/src/core/metadata'; -import {forwardRef, Binding, Inject, Optional} from 'angular2/src/core/di'; +import {forwardRef, Provider, Inject, Optional} from 'angular2/src/core/di'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor'; import {NgControl} from './ng_control'; import {Control} from '../model'; import {Validators, NG_VALIDATORS} from '../validators'; import {setUpControl, isPropertyUpdated, selectValueAccessor} from './shared'; -const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgModel)})); +const formControlBinding = + CONST_EXPR(new Provider(NgControl, {toAlias: forwardRef(() => NgModel)})); /** * Binds a domain model to a form control. diff --git a/modules/angular2/src/core/forms/directives/select_control_value_accessor.ts b/modules/angular2/src/core/forms/directives/select_control_value_accessor.ts index 235121f9e5..489ad8efa4 100644 --- a/modules/angular2/src/core/forms/directives/select_control_value_accessor.ts +++ b/modules/angular2/src/core/forms/directives/select_control_value_accessor.ts @@ -1,4 +1,4 @@ -import {Self, forwardRef, Binding} from 'angular2/src/core/di'; +import {Self, forwardRef, Provider} from 'angular2/src/core/di'; import {Renderer} from 'angular2/src/core/render'; import {ElementRef, QueryList} from 'angular2/src/core/linker'; import {Query, Directive} from 'angular2/src/core/metadata'; @@ -8,7 +8,7 @@ import {NG_VALUE_ACCESSOR, ControlValueAccessor} from './control_value_accessor' import {CONST_EXPR} from 'angular2/src/core/facade/lang'; import {setProperty} from './shared'; -const SELECT_VALUE_ACCESSOR = CONST_EXPR(new Binding( +const SELECT_VALUE_ACCESSOR = CONST_EXPR(new Provider( NG_VALUE_ACCESSOR, {toAlias: forwardRef(() => SelectControlValueAccessor), multi: true})); /** diff --git a/modules/angular2/src/core/forms/directives/validators.ts b/modules/angular2/src/core/forms/directives/validators.ts index 01ece4e391..95bb83a295 100644 --- a/modules/angular2/src/core/forms/directives/validators.ts +++ b/modules/angular2/src/core/forms/directives/validators.ts @@ -1,10 +1,10 @@ -import {forwardRef, Binding, OpaqueToken} from 'angular2/src/core/di'; +import {forwardRef, Provider, OpaqueToken} from 'angular2/src/core/di'; import {CONST_EXPR} from 'angular2/src/core/facade/lang'; import {Directive} from 'angular2/src/core/metadata'; import {Validators, NG_VALIDATORS} from '../validators'; const DEFAULT_VALIDATORS = - CONST_EXPR(new Binding(NG_VALIDATORS, {toValue: Validators.required, multi: true})); + CONST_EXPR(new Provider(NG_VALIDATORS, {toValue: Validators.required, multi: true})); @Directive({ selector: '[required][ng-control],[required][ng-form-control],[required][ng-model]', diff --git a/modules/angular2/src/core/forms/form_builder.ts b/modules/angular2/src/core/forms/form_builder.ts index c17c5b9ecc..e9326063d8 100644 --- a/modules/angular2/src/core/forms/form_builder.ts +++ b/modules/angular2/src/core/forms/form_builder.ts @@ -15,7 +15,7 @@ import * as modelModule from './model'; * * @Component({ * selector: 'login-comp', - * viewBindings: [FormBuilder] + * viewProviders: [FormBuilder] * }) * @View({ * template: ` diff --git a/modules/angular2/src/core/linker/directive_resolver.ts b/modules/angular2/src/core/linker/directive_resolver.ts index a5f71760e1..e3c4cc71c6 100644 --- a/modules/angular2/src/core/linker/directive_resolver.ts +++ b/modules/angular2/src/core/linker/directive_resolver.ts @@ -108,22 +108,18 @@ export class DirectiveResolver { var mergedQueries = isPresent(dm.queries) ? StringMapWrapper.merge(dm.queries, queries) : queries; - // TODO: remove after migrating from properties to inputs - if (mergedInputs.length == 0 && isPresent(dm.properties)) mergedInputs = dm.properties; - if (mergedOutputs.length == 0 && isPresent(dm.events)) mergedOutputs = dm.events; - if (dm instanceof ComponentMetadata) { return new ComponentMetadata({ selector: dm.selector, inputs: mergedInputs, outputs: mergedOutputs, host: mergedHost, - bindings: dm.bindings, exportAs: dm.exportAs, moduleId: dm.moduleId, queries: mergedQueries, changeDetection: dm.changeDetection, - viewBindings: dm.viewBindings + providers: dm.providers, + viewProviders: dm.viewProviders }); } else { @@ -132,10 +128,10 @@ export class DirectiveResolver { inputs: mergedInputs, outputs: mergedOutputs, host: mergedHost, - bindings: dm.bindings, exportAs: dm.exportAs, moduleId: dm.moduleId, - queries: mergedQueries + queries: mergedQueries, + providers: dm.providers }); } } diff --git a/modules/angular2/src/core/linker/dynamic_component_loader.ts b/modules/angular2/src/core/linker/dynamic_component_loader.ts index dde6be616c..2f3aa8ebf8 100644 --- a/modules/angular2/src/core/linker/dynamic_component_loader.ts +++ b/modules/angular2/src/core/linker/dynamic_component_loader.ts @@ -1,4 +1,4 @@ -import {Key, Injector, ResolvedBinding, Binding, bind, Injectable} from 'angular2/src/core/di'; +import {Key, Injector, ResolvedProvider, Provider, provide, Injectable} from 'angular2/src/core/di'; import {Compiler} from './compiler'; import {isType, Type, stringify, isPresent} from 'angular2/src/core/facade/lang'; import {Promise} from 'angular2/src/core/facade/async'; @@ -161,7 +161,7 @@ export abstract class DynamicComponentLoader { * location within the Component View of this Component Instance is specified via `anchorName` * Template Variable Name. * - * You can optionally provide `bindings` to configure the {@link Injector} provisioned for this + * You can optionally provide `providers` to configure the {@link Injector} provisioned for this * Component Instance. * * Returns a promise for the {@link ComponentRef} representing the newly created Component. @@ -209,13 +209,13 @@ export abstract class DynamicComponentLoader { * ``` */ abstract loadIntoLocation(type: Type, hostLocation: ElementRef, anchorName: string, - bindings?: ResolvedBinding[]): Promise; + providers?: ResolvedProvider[]): Promise; /** * Creates an instance of a Component and attaches it to the View Container found at the * `location` specified as {@link ElementRef}. * - * You can optionally provide `bindings` to configure the {@link Injector} provisioned for this + * You can optionally provide `providers` to configure the {@link Injector} provisioned for this * Component Instance. * * Returns a promise for the {@link ComponentRef} representing the newly created Component. @@ -256,7 +256,7 @@ export abstract class DynamicComponentLoader { * Child * ``` */ - abstract loadNextToLocation(type: Type, location: ElementRef, bindings?: ResolvedBinding[]): + abstract loadNextToLocation(type: Type, location: ElementRef, providers?: ResolvedProvider[]): Promise; } @@ -283,17 +283,18 @@ export class DynamicComponentLoader_ extends DynamicComponentLoader { } loadIntoLocation(type: Type, hostLocation: ElementRef, anchorName: string, - bindings: ResolvedBinding[] = null): Promise { + providers: ResolvedProvider[] = null): Promise { return this.loadNextToLocation( - type, this._viewManager.getNamedElementInComponentView(hostLocation, anchorName), bindings); + type, this._viewManager.getNamedElementInComponentView(hostLocation, anchorName), + providers); } loadNextToLocation(type: Type, location: ElementRef, - bindings: ResolvedBinding[] = null): Promise { + providers: ResolvedProvider[] = null): Promise { return this._compiler.compileInHost(type).then(hostProtoViewRef => { var viewContainer = this._viewManager.getViewContainer(location); var hostViewRef = - viewContainer.createHostView(hostProtoViewRef, viewContainer.length, bindings); + viewContainer.createHostView(hostProtoViewRef, viewContainer.length, providers); var newLocation = this._viewManager.getHostElement(hostViewRef); var component = this._viewManager.getComponent(newLocation); diff --git a/modules/angular2/src/core/linker/element_binder.ts b/modules/angular2/src/core/linker/element_binder.ts index 4dbc1799cd..aef6e5d632 100644 --- a/modules/angular2/src/core/linker/element_binder.ts +++ b/modules/angular2/src/core/linker/element_binder.ts @@ -1,13 +1,13 @@ import {isBlank} from 'angular2/src/core/facade/lang'; import {BaseException} from 'angular2/src/core/facade/exceptions'; import * as eiModule from './element_injector'; -import {DirectiveBinding} from './element_injector'; +import {DirectiveProvider} from './element_injector'; import * as viewModule from './view'; export class ElementBinder { constructor(public index: number, public parent: ElementBinder, public distanceToParent: number, public protoElementInjector: eiModule.ProtoElementInjector, - public componentDirective: DirectiveBinding, + public componentDirective: DirectiveProvider, public nestedProtoView: viewModule.AppProtoView) { if (isBlank(index)) { throw new BaseException('null index not allowed.'); diff --git a/modules/angular2/src/core/linker/element_injector.ts b/modules/angular2/src/core/linker/element_injector.ts index 61c1729437..6de0ccb4d2 100644 --- a/modules/angular2/src/core/linker/element_injector.ts +++ b/modules/angular2/src/core/linker/element_injector.ts @@ -13,11 +13,11 @@ import { Injector, Key, Dependency, - bind, - Binding, - ResolvedBinding, - NoBindingError, - AbstractBindingError, + provide, + Provider, + ResolvedProvider, + NoProviderError, + AbstractProviderError, CyclicDependencyError, resolveForwardRef } from 'angular2/src/core/di'; @@ -27,10 +27,10 @@ import { Visibility, InjectorInlineStrategy, InjectorDynamicStrategy, - BindingWithVisibility, + ProviderWithVisibility, DependencyProvider } from 'angular2/src/core/di/injector'; -import {resolveBinding, ResolvedFactory, ResolvedBinding_} from 'angular2/src/core/di/binding'; +import {resolveProvider, ResolvedFactory, ResolvedProvider_} from 'angular2/src/core/di/provider'; import {AttributeMetadata, QueryMetadata} from '../metadata/di'; @@ -49,7 +49,7 @@ import {QueryList} from './query_list'; import {reflector} from 'angular2/src/core/reflection/reflection'; import {SetterFn} from 'angular2/src/core/reflection/types'; import {EventConfig} from 'angular2/src/core/linker/event_config'; -import {PipeBinding} from '../pipes/pipe_binding'; +import {PipeProvider} from 'angular2/src/core/pipes/pipe_provider'; import {LifecycleHooks} from './interfaces'; import {ViewContainerRef_} from "./view_container_ref"; @@ -129,12 +129,12 @@ export class DirectiveDependency extends Dependency { } } -export class DirectiveBinding extends ResolvedBinding_ { +export class DirectiveProvider extends ResolvedProvider_ { public callOnDestroy: boolean; constructor(key: Key, factory: Function, deps: Dependency[], public metadata: DirectiveMetadata, - public bindings: Array, - public viewBindings: Array) { + public providers: Array, + public viewProviders: Array) { super(key, [new ResolvedFactory(factory, deps)], false); this.callOnDestroy = hasLifecycleHook(LifecycleHooks.OnDestroy, key.token); } @@ -157,24 +157,25 @@ export class DirectiveBinding extends ResolvedBinding_ { []; } - static createFromBinding(binding: Binding, meta: DirectiveMetadata): DirectiveBinding { + static createFromProvider(provider: Provider, meta: DirectiveMetadata): DirectiveProvider { if (isBlank(meta)) { meta = new DirectiveMetadata(); } - var rb = resolveBinding(binding); + var rb = resolveProvider(provider); var rf = rb.resolvedFactories[0]; var deps = rf.dependencies.map(DirectiveDependency.createFrom); - var bindings = isPresent(meta.bindings) ? meta.bindings : []; - var viewBindigs = - meta instanceof ComponentMetadata && isPresent(meta.viewBindings) ? meta.viewBindings : []; - return new DirectiveBinding(rb.key, rf.factory, deps, meta, bindings, viewBindigs); + var providers = isPresent(meta.providers) ? meta.providers : []; + var viewBindigs = meta instanceof ComponentMetadata && isPresent(meta.viewProviders) ? + meta.viewProviders : + []; + return new DirectiveProvider(rb.key, rf.factory, deps, meta, providers, viewBindigs); } - static createFromType(type: Type, annotation: DirectiveMetadata): DirectiveBinding { - var binding = new Binding(type, {toClass: type}); - return DirectiveBinding.createFromBinding(binding, annotation); + static createFromType(type: Type, annotation: DirectiveMetadata): DirectiveProvider { + var provider = new Provider(type, {toClass: type}); + return DirectiveProvider.createFromProvider(provider, annotation); } } @@ -200,29 +201,29 @@ export class EventEmitterAccessor { } } -function _createEventEmitterAccessors(bwv: BindingWithVisibility): EventEmitterAccessor[] { - var binding = bwv.binding; - if (!(binding instanceof DirectiveBinding)) return []; - var db = binding; +function _createEventEmitterAccessors(bwv: ProviderWithVisibility): EventEmitterAccessor[] { + var provider = bwv.provider; + if (!(provider instanceof DirectiveProvider)) return []; + var db = provider; return db.eventEmitters.map(eventConfig => { var parsedEvent = EventConfig.parse(eventConfig); return new EventEmitterAccessor(parsedEvent.eventName, reflector.getter(parsedEvent.fieldName)); }); } -function _createProtoQueryRefs(bindings: BindingWithVisibility[]): ProtoQueryRef[] { +function _createProtoQueryRefs(providers: ProviderWithVisibility[]): ProtoQueryRef[] { var res = []; - ListWrapper.forEachWithIndex(bindings, (b, i) => { - if (b.binding instanceof DirectiveBinding) { - var directiveBinding = b.binding; + ListWrapper.forEachWithIndex(providers, (b, i) => { + if (b.provider instanceof DirectiveProvider) { + var directiveProvider = b.provider; // field queries - var queries: QueryMetadataWithSetter[] = directiveBinding.queries; + var queries: QueryMetadataWithSetter[] = directiveProvider.queries; queries.forEach(q => res.push(new ProtoQueryRef(i, q.setter, q.metadata))); // queries passed into the constructor. // TODO: remove this after constructor queries are no longer supported var deps: DirectiveDependency[] = - directiveBinding.resolvedFactory.dependencies; + directiveProvider.resolvedFactory.dependencies; deps.forEach(d => { if (isPresent(d.queryDecorator)) res.push(new ProtoQueryRef(i, null, d.queryDecorator)); }); @@ -238,67 +239,67 @@ export class ProtoElementInjector { protoQueryRefs: ProtoQueryRef[]; protoInjector: ProtoInjector; - static create(parent: ProtoElementInjector, index: number, bindings: DirectiveBinding[], - firstBindingIsComponent: boolean, distanceToParent: number, + static create(parent: ProtoElementInjector, index: number, providers: DirectiveProvider[], + firstProviderIsComponent: boolean, distanceToParent: number, directiveVariableBindings: Map): ProtoElementInjector { var bd = []; - ProtoElementInjector._createDirectiveBindingWithVisibility(bindings, bd, - firstBindingIsComponent); - if (firstBindingIsComponent) { - ProtoElementInjector._createViewBindingsWithVisibility(bindings, bd); + ProtoElementInjector._createDirectiveProviderWithVisibility(providers, bd, + firstProviderIsComponent); + if (firstProviderIsComponent) { + ProtoElementInjector._createViewProvidersWithVisibility(providers, bd); } - ProtoElementInjector._createBindingsWithVisibility(bindings, bd); - return new ProtoElementInjector(parent, index, bd, distanceToParent, firstBindingIsComponent, + ProtoElementInjector._createProvidersWithVisibility(providers, bd); + return new ProtoElementInjector(parent, index, bd, distanceToParent, firstProviderIsComponent, directiveVariableBindings); } - private static _createDirectiveBindingWithVisibility(dirBindings: DirectiveBinding[], - bd: BindingWithVisibility[], - firstBindingIsComponent: boolean) { - dirBindings.forEach(dirBinding => { - bd.push(ProtoElementInjector._createBindingWithVisibility(firstBindingIsComponent, dirBinding, - dirBindings, dirBinding)); + private static _createDirectiveProviderWithVisibility(dirProviders: DirectiveProvider[], + bd: ProviderWithVisibility[], + firstProviderIsComponent: boolean) { + dirProviders.forEach(dirProvider => { + bd.push(ProtoElementInjector._createProviderWithVisibility( + firstProviderIsComponent, dirProvider, dirProviders, dirProvider)); }); } - private static _createBindingsWithVisibility(dirBindings: DirectiveBinding[], - bd: BindingWithVisibility[]) { - var bindingsFromAllDirectives = []; - dirBindings.forEach(dirBinding => { - bindingsFromAllDirectives = - ListWrapper.concat(bindingsFromAllDirectives, dirBinding.bindings); + private static _createProvidersWithVisibility(dirProviders: DirectiveProvider[], + bd: ProviderWithVisibility[]) { + var providersFromAllDirectives = []; + dirProviders.forEach(dirProvider => { + providersFromAllDirectives = + ListWrapper.concat(providersFromAllDirectives, dirProvider.providers); }); - var resolved = Injector.resolve(bindingsFromAllDirectives); - resolved.forEach(b => bd.push(new BindingWithVisibility(b, Visibility.Public))); + var resolved = Injector.resolve(providersFromAllDirectives); + resolved.forEach(b => bd.push(new ProviderWithVisibility(b, Visibility.Public))); } - private static _createBindingWithVisibility(firstBindingIsComponent: boolean, - dirBinding: DirectiveBinding, - dirBindings: DirectiveBinding[], - binding: ResolvedBinding) { - var isComponent = firstBindingIsComponent && dirBindings[0] === dirBinding; - return new BindingWithVisibility(binding, - isComponent ? Visibility.PublicAndPrivate : Visibility.Public); + private static _createProviderWithVisibility(firstProviderIsComponent: boolean, + dirProvider: DirectiveProvider, + dirProviders: DirectiveProvider[], + provider: ResolvedProvider) { + var isComponent = firstProviderIsComponent && dirProviders[0] === dirProvider; + return new ProviderWithVisibility( + provider, isComponent ? Visibility.PublicAndPrivate : Visibility.Public); } - private static _createViewBindingsWithVisibility(dirBindings: DirectiveBinding[], - bd: BindingWithVisibility[]) { - var resolvedViewBindings = Injector.resolve(dirBindings[0].viewBindings); - resolvedViewBindings.forEach(b => bd.push(new BindingWithVisibility(b, Visibility.Private))); + private static _createViewProvidersWithVisibility(dirProviders: DirectiveProvider[], + bd: ProviderWithVisibility[]) { + var resolvedViewProviders = Injector.resolve(dirProviders[0].viewProviders); + resolvedViewProviders.forEach(b => bd.push(new ProviderWithVisibility(b, Visibility.Private))); } /** @internal */ - public _firstBindingIsComponent: boolean; + public _firstProviderIsComponent: boolean; constructor(public parent: ProtoElementInjector, public index: number, - bwv: BindingWithVisibility[], public distanceToParent: number, - _firstBindingIsComponent: boolean, + bwv: ProviderWithVisibility[], public distanceToParent: number, + _firstProviderIsComponent: boolean, public directiveVariableBindings: Map) { - this._firstBindingIsComponent = _firstBindingIsComponent; + this._firstProviderIsComponent = _firstProviderIsComponent; var length = bwv.length; this.protoInjector = new ProtoInjector(bwv); this.eventEmitterAccessors = ListWrapper.createFixedSize(length); @@ -316,7 +317,7 @@ export class ProtoElementInjector { get hasBindings(): boolean { return this.eventEmitterAccessors.length > 0; } - getBindingAtIndex(index: number): any { return this.protoInjector.getBindingAtIndex(index); } + getProviderAtIndex(index: number): any { return this.protoInjector.getProviderAtIndex(index); } } class _Context { @@ -456,12 +457,12 @@ export class ElementInjector extends TreeNode implements Depend isComponentKey(key: Key): boolean { return this._strategy.isComponentKey(key); } - getDependency(injector: Injector, binding: ResolvedBinding, dep: Dependency): any { + getDependency(injector: Injector, provider: ResolvedProvider, dep: Dependency): any { var key: Key = dep.key; - if (binding instanceof DirectiveBinding) { + if (provider instanceof DirectiveProvider) { var dirDep = dep; - var dirBin = binding; + var dirProvider = provider; var staticKeys = StaticKeys.instance(); @@ -475,7 +476,7 @@ export class ElementInjector extends TreeNode implements Depend if (dirDep.key.id === StaticKeys.instance().changeDetectorRefId) { // We provide the component's view change detector to components and // the surrounding component's change detector to directives. - if (dirBin.metadata instanceof ComponentMetadata) { + if (dirProvider.metadata instanceof ComponentMetadata) { var componentView = this._preBuiltObjects.view.getNestedView( this._preBuiltObjects.elementRef.boundElementIndex); return componentView.changeDetector.ref; @@ -498,12 +499,12 @@ export class ElementInjector extends TreeNode implements Depend return null; } - throw new NoBindingError(null, dirDep.key); + throw new NoProviderError(null, dirDep.key); } return this._preBuiltObjects.templateRef; } - } else if (binding instanceof PipeBinding) { + } else if (provider instanceof PipeProvider) { if (dep.key.id === StaticKeys.instance().changeDetectorRefId) { var componentView = this._preBuiltObjects.view.getNestedView( this._preBuiltObjects.elementRef.boundElementIndex); @@ -753,7 +754,7 @@ interface _ElementInjectorStrategy { } /** - * Strategy used by the `ElementInjector` when the number of bindings is 10 or less. + * Strategy used by the `ElementInjector` when the number of providers is 10 or less. * In such a case, inlining fields is beneficial for performances. */ class ElementInjectorInlineStrategy implements _ElementInjectorStrategy { @@ -764,26 +765,26 @@ class ElementInjectorInlineStrategy implements _ElementInjectorStrategy { var p = i.protoStrategy; i.resetConstructionCounter(); - if (p.binding0 instanceof DirectiveBinding && isPresent(p.keyId0) && i.obj0 === UNDEFINED) - i.obj0 = i.instantiateBinding(p.binding0, p.visibility0); - if (p.binding1 instanceof DirectiveBinding && isPresent(p.keyId1) && i.obj1 === UNDEFINED) - i.obj1 = i.instantiateBinding(p.binding1, p.visibility1); - if (p.binding2 instanceof DirectiveBinding && isPresent(p.keyId2) && i.obj2 === UNDEFINED) - i.obj2 = i.instantiateBinding(p.binding2, p.visibility2); - if (p.binding3 instanceof DirectiveBinding && isPresent(p.keyId3) && i.obj3 === UNDEFINED) - i.obj3 = i.instantiateBinding(p.binding3, p.visibility3); - if (p.binding4 instanceof DirectiveBinding && isPresent(p.keyId4) && i.obj4 === UNDEFINED) - i.obj4 = i.instantiateBinding(p.binding4, p.visibility4); - if (p.binding5 instanceof DirectiveBinding && isPresent(p.keyId5) && i.obj5 === UNDEFINED) - i.obj5 = i.instantiateBinding(p.binding5, p.visibility5); - if (p.binding6 instanceof DirectiveBinding && isPresent(p.keyId6) && i.obj6 === UNDEFINED) - i.obj6 = i.instantiateBinding(p.binding6, p.visibility6); - if (p.binding7 instanceof DirectiveBinding && isPresent(p.keyId7) && i.obj7 === UNDEFINED) - i.obj7 = i.instantiateBinding(p.binding7, p.visibility7); - if (p.binding8 instanceof DirectiveBinding && isPresent(p.keyId8) && i.obj8 === UNDEFINED) - i.obj8 = i.instantiateBinding(p.binding8, p.visibility8); - if (p.binding9 instanceof DirectiveBinding && isPresent(p.keyId9) && i.obj9 === UNDEFINED) - i.obj9 = i.instantiateBinding(p.binding9, p.visibility9); + if (p.provider0 instanceof DirectiveProvider && isPresent(p.keyId0) && i.obj0 === UNDEFINED) + i.obj0 = i.instantiateProvider(p.provider0, p.visibility0); + if (p.provider1 instanceof DirectiveProvider && isPresent(p.keyId1) && i.obj1 === UNDEFINED) + i.obj1 = i.instantiateProvider(p.provider1, p.visibility1); + if (p.provider2 instanceof DirectiveProvider && isPresent(p.keyId2) && i.obj2 === UNDEFINED) + i.obj2 = i.instantiateProvider(p.provider2, p.visibility2); + if (p.provider3 instanceof DirectiveProvider && isPresent(p.keyId3) && i.obj3 === UNDEFINED) + i.obj3 = i.instantiateProvider(p.provider3, p.visibility3); + if (p.provider4 instanceof DirectiveProvider && isPresent(p.keyId4) && i.obj4 === UNDEFINED) + i.obj4 = i.instantiateProvider(p.provider4, p.visibility4); + if (p.provider5 instanceof DirectiveProvider && isPresent(p.keyId5) && i.obj5 === UNDEFINED) + i.obj5 = i.instantiateProvider(p.provider5, p.visibility5); + if (p.provider6 instanceof DirectiveProvider && isPresent(p.keyId6) && i.obj6 === UNDEFINED) + i.obj6 = i.instantiateProvider(p.provider6, p.visibility6); + if (p.provider7 instanceof DirectiveProvider && isPresent(p.keyId7) && i.obj7 === UNDEFINED) + i.obj7 = i.instantiateProvider(p.provider7, p.visibility7); + if (p.provider8 instanceof DirectiveProvider && isPresent(p.keyId8) && i.obj8 === UNDEFINED) + i.obj8 = i.instantiateProvider(p.provider8, p.visibility8); + if (p.provider9 instanceof DirectiveProvider && isPresent(p.keyId9) && i.obj9 === UNDEFINED) + i.obj9 = i.instantiateProvider(p.provider9, p.visibility9); } dehydrate() { @@ -805,34 +806,44 @@ class ElementInjectorInlineStrategy implements _ElementInjectorStrategy { var i = this.injectorStrategy; var p = i.protoStrategy; - if (p.binding0 instanceof DirectiveBinding && (p.binding0).callOnDestroy) { + if (p.provider0 instanceof DirectiveProvider && + (p.provider0).callOnDestroy) { i.obj0.onDestroy(); } - if (p.binding1 instanceof DirectiveBinding && (p.binding1).callOnDestroy) { + if (p.provider1 instanceof DirectiveProvider && + (p.provider1).callOnDestroy) { i.obj1.onDestroy(); } - if (p.binding2 instanceof DirectiveBinding && (p.binding2).callOnDestroy) { + if (p.provider2 instanceof DirectiveProvider && + (p.provider2).callOnDestroy) { i.obj2.onDestroy(); } - if (p.binding3 instanceof DirectiveBinding && (p.binding3).callOnDestroy) { + if (p.provider3 instanceof DirectiveProvider && + (p.provider3).callOnDestroy) { i.obj3.onDestroy(); } - if (p.binding4 instanceof DirectiveBinding && (p.binding4).callOnDestroy) { + if (p.provider4 instanceof DirectiveProvider && + (p.provider4).callOnDestroy) { i.obj4.onDestroy(); } - if (p.binding5 instanceof DirectiveBinding && (p.binding5).callOnDestroy) { + if (p.provider5 instanceof DirectiveProvider && + (p.provider5).callOnDestroy) { i.obj5.onDestroy(); } - if (p.binding6 instanceof DirectiveBinding && (p.binding6).callOnDestroy) { + if (p.provider6 instanceof DirectiveProvider && + (p.provider6).callOnDestroy) { i.obj6.onDestroy(); } - if (p.binding7 instanceof DirectiveBinding && (p.binding7).callOnDestroy) { + if (p.provider7 instanceof DirectiveProvider && + (p.provider7).callOnDestroy) { i.obj7.onDestroy(); } - if (p.binding8 instanceof DirectiveBinding && (p.binding8).callOnDestroy) { + if (p.provider8 instanceof DirectiveProvider && + (p.provider8).callOnDestroy) { i.obj8.onDestroy(); } - if (p.binding9 instanceof DirectiveBinding && (p.binding9).callOnDestroy) { + if (p.provider9 instanceof DirectiveProvider && + (p.provider9).callOnDestroy) { i.obj9.onDestroy(); } } @@ -840,7 +851,7 @@ class ElementInjectorInlineStrategy implements _ElementInjectorStrategy { getComponent(): any { return this.injectorStrategy.obj0; } isComponentKey(key: Key): boolean { - return this._ei._proto._firstBindingIsComponent && isPresent(key) && + return this._ei._proto._firstProviderIsComponent && isPresent(key) && key.id === this.injectorStrategy.protoStrategy.keyId0; } @@ -848,51 +859,51 @@ class ElementInjectorInlineStrategy implements _ElementInjectorStrategy { var i = this.injectorStrategy; var p = i.protoStrategy; - if (isPresent(p.binding0) && p.binding0.key.token === query.selector) { - if (i.obj0 === UNDEFINED) i.obj0 = i.instantiateBinding(p.binding0, p.visibility0); + if (isPresent(p.provider0) && p.provider0.key.token === query.selector) { + if (i.obj0 === UNDEFINED) i.obj0 = i.instantiateProvider(p.provider0, p.visibility0); list.push(i.obj0); } - if (isPresent(p.binding1) && p.binding1.key.token === query.selector) { - if (i.obj1 === UNDEFINED) i.obj1 = i.instantiateBinding(p.binding1, p.visibility1); + if (isPresent(p.provider1) && p.provider1.key.token === query.selector) { + if (i.obj1 === UNDEFINED) i.obj1 = i.instantiateProvider(p.provider1, p.visibility1); list.push(i.obj1); } - if (isPresent(p.binding2) && p.binding2.key.token === query.selector) { - if (i.obj2 === UNDEFINED) i.obj2 = i.instantiateBinding(p.binding2, p.visibility2); + if (isPresent(p.provider2) && p.provider2.key.token === query.selector) { + if (i.obj2 === UNDEFINED) i.obj2 = i.instantiateProvider(p.provider2, p.visibility2); list.push(i.obj2); } - if (isPresent(p.binding3) && p.binding3.key.token === query.selector) { - if (i.obj3 === UNDEFINED) i.obj3 = i.instantiateBinding(p.binding3, p.visibility3); + if (isPresent(p.provider3) && p.provider3.key.token === query.selector) { + if (i.obj3 === UNDEFINED) i.obj3 = i.instantiateProvider(p.provider3, p.visibility3); list.push(i.obj3); } - if (isPresent(p.binding4) && p.binding4.key.token === query.selector) { - if (i.obj4 === UNDEFINED) i.obj4 = i.instantiateBinding(p.binding4, p.visibility4); + if (isPresent(p.provider4) && p.provider4.key.token === query.selector) { + if (i.obj4 === UNDEFINED) i.obj4 = i.instantiateProvider(p.provider4, p.visibility4); list.push(i.obj4); } - if (isPresent(p.binding5) && p.binding5.key.token === query.selector) { - if (i.obj5 === UNDEFINED) i.obj5 = i.instantiateBinding(p.binding5, p.visibility5); + if (isPresent(p.provider5) && p.provider5.key.token === query.selector) { + if (i.obj5 === UNDEFINED) i.obj5 = i.instantiateProvider(p.provider5, p.visibility5); list.push(i.obj5); } - if (isPresent(p.binding6) && p.binding6.key.token === query.selector) { - if (i.obj6 === UNDEFINED) i.obj6 = i.instantiateBinding(p.binding6, p.visibility6); + if (isPresent(p.provider6) && p.provider6.key.token === query.selector) { + if (i.obj6 === UNDEFINED) i.obj6 = i.instantiateProvider(p.provider6, p.visibility6); list.push(i.obj6); } - if (isPresent(p.binding7) && p.binding7.key.token === query.selector) { - if (i.obj7 === UNDEFINED) i.obj7 = i.instantiateBinding(p.binding7, p.visibility7); + if (isPresent(p.provider7) && p.provider7.key.token === query.selector) { + if (i.obj7 === UNDEFINED) i.obj7 = i.instantiateProvider(p.provider7, p.visibility7); list.push(i.obj7); } - if (isPresent(p.binding8) && p.binding8.key.token === query.selector) { - if (i.obj8 === UNDEFINED) i.obj8 = i.instantiateBinding(p.binding8, p.visibility8); + if (isPresent(p.provider8) && p.provider8.key.token === query.selector) { + if (i.obj8 === UNDEFINED) i.obj8 = i.instantiateProvider(p.provider8, p.visibility8); list.push(i.obj8); } - if (isPresent(p.binding9) && p.binding9.key.token === query.selector) { - if (i.obj9 === UNDEFINED) i.obj9 = i.instantiateBinding(p.binding9, p.visibility9); + if (isPresent(p.provider9) && p.provider9.key.token === query.selector) { + if (i.obj9 === UNDEFINED) i.obj9 = i.instantiateProvider(p.provider9, p.visibility9); list.push(i.obj9); } } } /** - * Strategy used by the `ElementInjector` when the number of bindings is 10 or less. + * Strategy used by the `ElementInjector` when the number of providers is 10 or less. * In such a case, inlining fields is beneficial for performances. */ class ElementInjectorDynamicStrategy implements _ElementInjectorStrategy { @@ -904,9 +915,9 @@ class ElementInjectorDynamicStrategy implements _ElementInjectorStrategy { inj.resetConstructionCounter(); for (var i = 0; i < p.keyIds.length; i++) { - if (p.bindings[i] instanceof DirectiveBinding && isPresent(p.keyIds[i]) && + if (p.providers[i] instanceof DirectiveProvider && isPresent(p.keyIds[i]) && inj.objs[i] === UNDEFINED) { - inj.objs[i] = inj.instantiateBinding(p.bindings[i], p.visibilities[i]); + inj.objs[i] = inj.instantiateProvider(p.providers[i], p.visibilities[i]); } } } @@ -920,9 +931,9 @@ class ElementInjectorDynamicStrategy implements _ElementInjectorStrategy { var ist = this.injectorStrategy; var p = ist.protoStrategy; - for (var i = 0; i < p.bindings.length; i++) { - if (p.bindings[i] instanceof DirectiveBinding && - (p.bindings[i]).callOnDestroy) { + for (var i = 0; i < p.providers.length; i++) { + if (p.providers[i] instanceof DirectiveProvider && + (p.providers[i]).callOnDestroy) { ist.objs[i].onDestroy(); } } @@ -932,17 +943,17 @@ class ElementInjectorDynamicStrategy implements _ElementInjectorStrategy { isComponentKey(key: Key): boolean { var p = this.injectorStrategy.protoStrategy; - return this._ei._proto._firstBindingIsComponent && isPresent(key) && key.id === p.keyIds[0]; + return this._ei._proto._firstProviderIsComponent && isPresent(key) && key.id === p.keyIds[0]; } addDirectivesMatchingQuery(query: QueryMetadata, list: any[]): void { var ist = this.injectorStrategy; var p = ist.protoStrategy; - for (var i = 0; i < p.bindings.length; i++) { - if (p.bindings[i].key.token === query.selector) { + for (var i = 0; i < p.providers.length; i++) { + if (p.providers[i].key.token === query.selector) { if (ist.objs[i] === UNDEFINED) { - ist.objs[i] = ist.instantiateBinding(p.bindings[i], p.visibilities[i]); + ist.objs[i] = ist.instantiateProvider(p.providers[i], p.visibilities[i]); } list.push(ist.objs[i]); } @@ -1025,7 +1036,7 @@ export class QueryRef { private _visitInjector(inj: ElementInjector, aggregator: any[]) { if (this.protoQueryRef.query.isVarBindingQuery) { - this._aggregateVariableBindings(inj, aggregator); + this._aggregateVariableBinding(inj, aggregator); } else { this._aggregateDirective(inj, aggregator); } @@ -1049,7 +1060,7 @@ export class QueryRef { } } - private _aggregateVariableBindings(inj: ElementInjector, aggregator: any[]): void { + private _aggregateVariableBinding(inj: ElementInjector, aggregator: any[]): void { var vb = this.protoQueryRef.query.varBindings; for (var i = 0; i < vb.length; ++i) { if (inj.hasVariableBinding(vb[i])) { diff --git a/modules/angular2/src/core/linker/proto_view_factory.ts b/modules/angular2/src/core/linker/proto_view_factory.ts index 16f3b69dea..4df1fa23a3 100644 --- a/modules/angular2/src/core/linker/proto_view_factory.ts +++ b/modules/angular2/src/core/linker/proto_view_factory.ts @@ -3,14 +3,14 @@ import {isPresent, isBlank, Type, isArray, isNumber} from 'angular2/src/core/fac import {RenderProtoViewRef} from 'angular2/src/core/render/api'; -import {Injectable, Binding, resolveForwardRef, Inject} from 'angular2/src/core/di'; +import {Injectable, Provider, resolveForwardRef, Inject} from 'angular2/src/core/di'; -import {PipeBinding} from '../pipes/pipe_binding'; +import {PipeProvider} from '../pipes/pipe_provider'; import {ProtoPipes} from '../pipes/pipes'; import {AppProtoView, AppProtoViewMergeInfo, ViewType} from './view'; import {ElementBinder} from './element_binder'; -import {ProtoElementInjector, DirectiveBinding} from './element_injector'; +import {ProtoElementInjector, DirectiveProvider} from './element_injector'; import {DirectiveResolver} from './directive_resolver'; import {ViewResolver} from './view_resolver'; import {PipeResolver} from './pipe_resolver'; @@ -55,7 +55,7 @@ export class ProtoViewFactory { var result = this._cache.get(compiledTemplate.id); if (isBlank(result)) { var templateData = compiledTemplate.getData(this._appId); - var emptyMap: {[key: string]: PipeBinding} = {}; + var emptyMap: {[key: string]: PipeProvider} = {}; result = new AppProtoView(templateData.commands, ViewType.HOST, true, templateData.changeDetectorFactory, null, new ProtoPipes(emptyMap)); this._cache.set(compiledTemplate.id, result); @@ -76,7 +76,7 @@ export class ProtoViewFactory { nestedProtoView = new AppProtoView(compiledTemplateData.commands, ViewType.COMPONENT, true, compiledTemplateData.changeDetectorFactory, null, - ProtoPipes.fromBindings(boundPipes)); + ProtoPipes.fromProviders(boundPipes)); // Note: The cache is updated before recursing // to be able to resolve cycles this._cache.set(cmd.template.id, nestedProtoView); @@ -112,9 +112,9 @@ export class ProtoViewFactory { initializer.variableLocations); } - private _bindPipe(typeOrBinding): PipeBinding { - let meta = this._pipeResolver.resolve(typeOrBinding); - return PipeBinding.createFromType(typeOrBinding, meta); + private _bindPipe(typeOrProvider): PipeProvider { + let meta = this._pipeResolver.resolve(typeOrProvider); + return PipeProvider.createFromType(typeOrProvider, meta); } private _flattenPipes(view: ViewMetadata): any[] { @@ -245,12 +245,12 @@ function _createElementBinder(directiveResolver: DirectiveResolver, nestedProtoV if (isBlank(parentProtoElementInjector)) { distanceToParentPei = -1; } - var componentDirectiveBinding: DirectiveBinding = null; + var componentDirectiveProvider: DirectiveProvider = null; var isEmbeddedTemplate = false; - var directiveBindings: DirectiveBinding[] = - beginElementCmd.directives.map(type => bindDirective(directiveResolver, type)); + var directiveProviders: DirectiveProvider[] = + beginElementCmd.directives.map(type => provideDirective(directiveResolver, type)); if (beginElementCmd instanceof BeginComponentCmd) { - componentDirectiveBinding = directiveBindings[0]; + componentDirectiveProvider = directiveProviders[0]; } else if (beginElementCmd instanceof EmbeddedTemplateCmd) { isEmbeddedTemplate = true; } @@ -262,29 +262,29 @@ function _createElementBinder(directiveResolver: DirectiveResolver, nestedProtoV // so that, when hydrating, $implicit can be set to the element. // -