2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-05-31 15:22:59 -07:00
|
|
|
import {BaseException} from '../facade/exceptions';
|
2016-08-10 18:21:28 -07:00
|
|
|
import {isBlank, isFunction, isType, normalizeBool, stringify} from '../facade/lang';
|
|
|
|
import {Type} from '../type';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
|
|
|
|
/**
|
2015-09-17 13:12:50 -07:00
|
|
|
* Describes how the {@link Injector} should instantiate a given token.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-10-10 22:11:13 -07:00
|
|
|
* See {@link provide}.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/GNAyj6K6PfYg2NBzgwZ5?p%3Dpreview&p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
2015-10-12 11:30:34 -07:00
|
|
|
* new Provider("message", { useValue: 'Hello' })
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* expect(injector.get("message")).toEqual('Hello');
|
2015-04-15 22:35:38 +00:00
|
|
|
* ```
|
2016-05-25 15:00:05 -07:00
|
|
|
* @deprecated
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
export class Provider {
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-10-11 14:42:15 -07:00
|
|
|
* Token used when retrieving this provider. Usually, it is a type {@link Type}.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2016-06-28 11:35:59 -07:00
|
|
|
token: any;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-17 13:12:50 -07:00
|
|
|
* Binds a DI token to an implementation class.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/RSTG86qgmoxCyj9SWPwY?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-10-14 08:21:05 -03:00
|
|
|
* Because `useExisting` and `useClass` are often confused, the example contains
|
|
|
|
* both use cases for easy comparison.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-04-15 22:35:38 +00:00
|
|
|
* class Vehicle {}
|
|
|
|
*
|
|
|
|
* class Car extends Vehicle {}
|
|
|
|
*
|
|
|
|
* var injectorClass = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
2016-04-25 22:25:21 -07:00
|
|
|
* {provide: Vehicle, useClass: Car }
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
* var injectorAlias = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
2016-04-25 22:25:21 -07:00
|
|
|
* {provide: Vehicle, useExisting: Car }
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
|
|
|
|
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
*
|
|
|
|
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
|
|
|
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
* ```
|
|
|
|
*/
|
2016-08-10 18:21:28 -07:00
|
|
|
useClass: Type<any>;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-17 13:12:50 -07:00
|
|
|
* Binds a DI token to a value.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/UFVsMVQIDe7l4waWziES?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
2015-10-12 11:30:34 -07:00
|
|
|
* new Provider("message", { useValue: 'Hello' })
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* expect(injector.get("message")).toEqual('Hello');
|
2015-04-15 22:35:38 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2016-06-28 11:35:59 -07:00
|
|
|
useValue: any;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-12 11:30:34 -07:00
|
|
|
* Binds a DI token to an existing token.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-10-12 11:30:34 -07:00
|
|
|
* {@link Injector} returns the same instance as if the provided token was used.
|
|
|
|
* This is in contrast to `useClass` where a separate instance of `useClass` is returned.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/QsatsOJJ6P8T2fMe9gr8?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-10-14 08:21:05 -03:00
|
|
|
* Because `useExisting` and `useClass` are often confused the example contains
|
|
|
|
* both use cases for easy comparison.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-04-15 22:35:38 +00:00
|
|
|
* class Vehicle {}
|
|
|
|
*
|
|
|
|
* class Car extends Vehicle {}
|
|
|
|
*
|
|
|
|
* var injectorAlias = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
2016-04-25 22:25:21 -07:00
|
|
|
* {provide: Vehicle, useExisting: Car }
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
* var injectorClass = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
2016-04-25 22:25:21 -07:00
|
|
|
* {provide: Vehicle, useClass: Car }
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
|
|
|
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
|
|
|
|
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
* ```
|
|
|
|
*/
|
2016-06-28 11:35:59 -07:00
|
|
|
useExisting: any;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-17 13:12:50 -07:00
|
|
|
* Binds a DI token to a function which computes the value.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/Scoxy0pJNqKGAPZY1VVC?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-04-15 22:35:38 +00:00
|
|
|
* var injector = Injector.resolveAndCreate([
|
2016-04-25 22:25:21 -07:00
|
|
|
* {provide: Number, useFactory: () => { return 1+2; }},
|
2015-10-12 11:30:34 -07:00
|
|
|
* new Provider(String, { useFactory: (value) => { return "Value: " + value; },
|
2015-09-17 13:12:50 -07:00
|
|
|
* deps: [Number] })
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(Number)).toEqual(3);
|
|
|
|
* expect(injector.get(String)).toEqual('Value: 3');
|
|
|
|
* ```
|
2015-09-17 13:12:50 -07:00
|
|
|
*
|
2015-12-16 15:47:48 +08:00
|
|
|
* Used in conjunction with dependencies.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2015-10-12 11:30:34 -07:00
|
|
|
useFactory: Function;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-17 13:12:50 -07:00
|
|
|
* Specifies a set of dependencies
|
2015-04-24 15:19:11 -07:00
|
|
|
* (as `token`s) which should be injected into the factory function.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/Scoxy0pJNqKGAPZY1VVC?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-04-15 22:35:38 +00:00
|
|
|
* var injector = Injector.resolveAndCreate([
|
2016-04-25 22:25:21 -07:00
|
|
|
* {provide: Number, useFactory: () => { return 1+2; }},
|
2015-10-12 11:30:34 -07:00
|
|
|
* new Provider(String, { useFactory: (value) => { return "Value: " + value; },
|
2015-09-17 13:12:50 -07:00
|
|
|
* deps: [Number] })
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(Number)).toEqual(3);
|
|
|
|
* expect(injector.get(String)).toEqual('Value: 3');
|
|
|
|
* ```
|
2015-09-17 13:12:50 -07:00
|
|
|
*
|
2015-10-12 11:30:34 -07:00
|
|
|
* Used in conjunction with `useFactory`.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2015-09-02 10:21:28 -07:00
|
|
|
dependencies: Object[];
|
2015-04-24 15:19:11 -07:00
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-09-02 10:21:28 -07:00
|
|
|
_multi: boolean;
|
|
|
|
|
2016-06-28 11:35:59 -07:00
|
|
|
constructor(token: any, {useClass, useValue, useExisting, useFactory, deps, multi}: {
|
2016-08-10 18:21:28 -07:00
|
|
|
useClass?: Type<any>,
|
2016-06-28 11:35:59 -07:00
|
|
|
useValue?: any,
|
|
|
|
useExisting?: any,
|
|
|
|
useFactory?: Function,
|
|
|
|
deps?: Object[],
|
|
|
|
multi?: boolean
|
|
|
|
}) {
|
2015-04-09 23:17:05 -07:00
|
|
|
this.token = token;
|
2015-10-12 11:30:34 -07:00
|
|
|
this.useClass = useClass;
|
|
|
|
this.useValue = useValue;
|
|
|
|
this.useExisting = useExisting;
|
|
|
|
this.useFactory = useFactory;
|
2015-04-09 23:17:05 -07:00
|
|
|
this.dependencies = deps;
|
2015-09-02 10:21:28 -07:00
|
|
|
this._multi = multi;
|
2015-04-09 23:17:05 -07:00
|
|
|
}
|
|
|
|
|
2015-09-17 13:12:50 -07:00
|
|
|
// TODO: Provide a full working example after alpha38 is released.
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-10-10 22:11:13 -07:00
|
|
|
* Creates multiple providers matching the same token (a multi-provider).
|
2015-09-02 10:21:28 -07:00
|
|
|
*
|
2015-10-10 22:11:13 -07:00
|
|
|
* Multi-providers are used for creating pluggable service, where the system comes
|
2015-12-16 15:47:48 +08:00
|
|
|
* with some default providers, and the user can register additional providers.
|
2015-10-10 22:11:13 -07:00
|
|
|
* The combination of the default providers and the additional providers will be
|
2015-09-21 14:19:03 -07:00
|
|
|
* used to drive the behavior of the system.
|
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example
|
2015-09-02 10:21:28 -07:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-09-02 10:21:28 -07:00
|
|
|
* var injector = Injector.resolveAndCreate([
|
2015-10-12 11:30:34 -07:00
|
|
|
* new Provider("Strings", { useValue: "String1", multi: true}),
|
|
|
|
* new Provider("Strings", { useValue: "String2", multi: true})
|
2015-09-02 10:21:28 -07:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get("Strings")).toEqual(["String1", "String2"]);
|
|
|
|
* ```
|
|
|
|
*
|
2015-10-10 22:11:13 -07:00
|
|
|
* Multi-providers and regular providers cannot be mixed. The following
|
2015-09-02 10:21:28 -07:00
|
|
|
* will throw an exception:
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-09-02 10:21:28 -07:00
|
|
|
* var injector = Injector.resolveAndCreate([
|
2015-10-12 11:30:34 -07:00
|
|
|
* new Provider("Strings", { useValue: "String1", multi: true }),
|
|
|
|
* new Provider("Strings", { useValue: "String2"})
|
2015-09-02 10:21:28 -07:00
|
|
|
* ]);
|
|
|
|
* ```
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2015-09-02 10:21:28 -07:00
|
|
|
get multi(): boolean { return normalizeBool(this._multi); }
|
2015-04-09 23:17:05 -07:00
|
|
|
}
|
|
|
|
|
2015-10-10 22:11:13 -07:00
|
|
|
/**
|
2015-12-03 15:49:09 -08:00
|
|
|
* See {@link Provider} instead.
|
|
|
|
*
|
2015-10-10 22:11:13 -07:00
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
export class Binding extends Provider {
|
2016-06-28 11:35:59 -07:00
|
|
|
constructor(token: any, {toClass, toValue, toAlias, toFactory, deps, multi}: {
|
2016-08-10 18:21:28 -07:00
|
|
|
toClass?: Type<any>,
|
2015-10-10 22:11:13 -07:00
|
|
|
toValue?: any,
|
|
|
|
toAlias?: any,
|
2015-10-12 11:30:34 -07:00
|
|
|
toFactory: Function, deps?: Object[], multi?: boolean
|
2015-10-10 22:11:13 -07:00
|
|
|
}) {
|
|
|
|
super(token, {
|
2015-10-12 11:30:34 -07:00
|
|
|
useClass: toClass,
|
|
|
|
useValue: toValue,
|
|
|
|
useExisting: toAlias,
|
|
|
|
useFactory: toFactory,
|
2015-10-10 22:11:13 -07:00
|
|
|
deps: deps,
|
|
|
|
multi: multi
|
|
|
|
});
|
|
|
|
}
|
2015-10-12 11:30:34 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
get toClass() { return this.useClass; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
get toAlias() { return this.useExisting; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
get toFactory() { return this.useFactory; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
get toValue() { return this.useValue; }
|
2015-10-10 22:11:13 -07:00
|
|
|
}
|
2015-09-17 13:12:50 -07:00
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
2015-10-10 22:11:13 -07:00
|
|
|
* Creates a {@link Provider}.
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-10-10 22:11:13 -07:00
|
|
|
* To construct a {@link Provider}, bind a `token` to either a class, a value, a factory function,
|
|
|
|
* or
|
2015-10-12 11:30:34 -07:00
|
|
|
* to an existing `token`.
|
2015-10-10 22:11:13 -07:00
|
|
|
* See {@link ProviderBuilder} for more details.
|
2015-09-03 16:17:23 -07:00
|
|
|
*
|
2016-07-07 23:02:35 -07:00
|
|
|
* The `token` is most commonly a class or {@link OpaqueToken}.
|
2015-12-03 15:49:09 -08:00
|
|
|
*
|
|
|
|
* @deprecated
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
2016-06-28 11:35:59 -07:00
|
|
|
export function bind(token: any): ProviderBuilder {
|
2015-10-10 22:11:13 -07:00
|
|
|
return new ProviderBuilder(token);
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* Helper class for the {@link bind} function.
|
2016-05-25 15:00:05 -07:00
|
|
|
* @deprecated
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
export class ProviderBuilder {
|
2016-06-28 11:35:59 -07:00
|
|
|
constructor(public token: any) {}
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-09-17 13:12:50 -07:00
|
|
|
* Binds a DI token to a class.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/ZpBCSYqv6e2ud5KXLdxQ?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-10-14 08:21:05 -03:00
|
|
|
* Because `toAlias` and `toClass` are often confused, the example contains
|
|
|
|
* both use cases for easy comparison.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-04-15 22:35:38 +00:00
|
|
|
* class Vehicle {}
|
|
|
|
*
|
|
|
|
* class Car extends Vehicle {}
|
|
|
|
*
|
|
|
|
* var injectorClass = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
2016-06-02 17:30:40 -07:00
|
|
|
* {provide: Vehicle, useClass: Car}
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
* var injectorAlias = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
2016-06-02 17:30:40 -07:00
|
|
|
* {provide: Vehicle, useExisting: Car}
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
|
|
|
|
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
*
|
|
|
|
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
|
|
|
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
* ```
|
|
|
|
*/
|
2016-08-10 18:21:28 -07:00
|
|
|
toClass(type: Type<any>): Provider {
|
2015-09-24 19:31:14 -07:00
|
|
|
if (!isType(type)) {
|
|
|
|
throw new BaseException(
|
2015-10-10 22:11:13 -07:00
|
|
|
`Trying to create a class provider but "${stringify(type)}" is not a class!`);
|
2015-09-24 19:31:14 -07:00
|
|
|
}
|
2015-10-12 11:30:34 -07:00
|
|
|
return new Provider(this.token, {useClass: type});
|
2015-09-24 19:31:14 -07:00
|
|
|
}
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-09-17 13:12:50 -07:00
|
|
|
* Binds a DI token to a value.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/G024PFHmDL0cJFgfZK8O?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-04-15 22:35:38 +00:00
|
|
|
* var injector = Injector.resolveAndCreate([
|
2016-06-02 17:30:40 -07:00
|
|
|
* {provide: 'message', useValue: 'Hello'}
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* expect(injector.get('message')).toEqual('Hello');
|
2015-04-15 22:35:38 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2015-10-12 11:30:34 -07:00
|
|
|
toValue(value: any): Provider { return new Provider(this.token, {useValue: value}); }
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-10-12 11:30:34 -07:00
|
|
|
* Binds a DI token to an existing token.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-10-12 11:30:34 -07:00
|
|
|
* Angular will return the same instance as if the provided token was used. (This is
|
|
|
|
* in contrast to `useClass` where a separate instance of `useClass` will be returned.)
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/uBaoF2pN5cfc5AfZapNw?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-10-14 08:21:05 -03:00
|
|
|
* Because `toAlias` and `toClass` are often confused, the example contains
|
|
|
|
* both use cases for easy comparison.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-04-15 22:35:38 +00:00
|
|
|
* class Vehicle {}
|
|
|
|
*
|
|
|
|
* class Car extends Vehicle {}
|
|
|
|
*
|
|
|
|
* var injectorAlias = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
2016-06-02 17:30:40 -07:00
|
|
|
* {provide: Vehicle, useExisting: Car}
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
* var injectorClass = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
2016-06-02 17:30:40 -07:00
|
|
|
* {provide: Vehicle, useClass: Car})
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
|
|
|
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
|
|
|
|
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
* ```
|
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
toAlias(aliasToken: /*Type*/ any): Provider {
|
2015-05-26 10:55:12 +02:00
|
|
|
if (isBlank(aliasToken)) {
|
|
|
|
throw new BaseException(`Can not alias ${stringify(this.token)} to a blank value!`);
|
|
|
|
}
|
2015-10-12 11:30:34 -07:00
|
|
|
return new Provider(this.token, {useExisting: aliasToken});
|
2015-05-26 10:55:12 +02:00
|
|
|
}
|
2015-02-21 15:18:06 +01:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-09-17 13:12:50 -07:00
|
|
|
* Binds a DI token to a function which computes the value.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/OejNIfTT3zb1iBxaIYOb?p=preview))
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-09-17 13:12:50 -07:00
|
|
|
* ```typescript
|
2015-04-15 22:35:38 +00:00
|
|
|
* var injector = Injector.resolveAndCreate([
|
2016-06-02 17:30:40 -07:00
|
|
|
* {provide: Number, useFactory: () => { return 1+2; }},
|
|
|
|
* {provide: String, useFactory: (v) => { return "Value: " + v; }, deps: [Number]}
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(Number)).toEqual(3);
|
|
|
|
* expect(injector.get(String)).toEqual('Value: 3');
|
|
|
|
* ```
|
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
toFactory(factory: Function, dependencies?: any[]): Provider {
|
2015-09-24 19:31:14 -07:00
|
|
|
if (!isFunction(factory)) {
|
|
|
|
throw new BaseException(
|
2015-10-10 22:11:13 -07:00
|
|
|
`Trying to create a factory provider but "${stringify(factory)}" is not a function!`);
|
2015-09-24 19:31:14 -07:00
|
|
|
}
|
2015-10-12 11:30:34 -07:00
|
|
|
return new Provider(this.token, {useFactory: factory, deps: dependencies});
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
2015-04-09 23:17:05 -07:00
|
|
|
}
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-09-02 10:21:28 -07:00
|
|
|
/**
|
2016-04-14 12:35:24 -07:00
|
|
|
* Creates a {@link Provider}.
|
2015-09-02 10:21:28 -07:00
|
|
|
*
|
2016-04-14 12:35:24 -07:00
|
|
|
* See {@link Provider} for more details.
|
|
|
|
*
|
|
|
|
* <!-- TODO: improve the docs -->
|
2016-05-25 15:00:05 -07:00
|
|
|
* @deprecated
|
2015-09-02 10:21:28 -07:00
|
|
|
*/
|
2016-06-28 11:35:59 -07:00
|
|
|
export function provide(token: any, {useClass, useValue, useExisting, useFactory, deps, multi}: {
|
2016-08-10 18:21:28 -07:00
|
|
|
useClass?: Type<any>,
|
2016-06-28 11:35:59 -07:00
|
|
|
useValue?: any,
|
|
|
|
useExisting?: any,
|
|
|
|
useFactory?: Function,
|
|
|
|
deps?: Object[],
|
|
|
|
multi?: boolean
|
|
|
|
}): Provider {
|
2016-04-14 12:35:24 -07:00
|
|
|
return new Provider(token, {
|
|
|
|
useClass: useClass,
|
|
|
|
useValue: useValue,
|
|
|
|
useExisting: useExisting,
|
|
|
|
useFactory: useFactory,
|
|
|
|
deps: deps,
|
|
|
|
multi: multi
|
2015-09-02 10:21:28 -07:00
|
|
|
});
|
2014-11-20 12:07:48 -08:00
|
|
|
}
|