2015-02-05 13:08:05 -08:00
|
|
|
import {CONST} from "angular2/src/facade/lang";
|
2014-10-03 20:34:37 -04:00
|
|
|
|
2014-10-14 16:00:35 -04:00
|
|
|
/**
|
|
|
|
* A parameter annotation that creates a synchronous eager dependency.
|
|
|
|
*
|
2014-12-04 11:01:19 +01:00
|
|
|
* ```
|
|
|
|
* class AComponent {
|
2015-04-15 22:35:38 +00:00
|
|
|
* constructor(@Inject(MyService) aService:MyService) {}
|
2014-12-04 11:01:19 +01:00
|
|
|
* }
|
|
|
|
* ```
|
2014-10-14 16:00:35 -04:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* @exportedAs angular2/di_annotations
|
2014-10-14 16:00:35 -04:00
|
|
|
*/
|
2014-10-03 20:34:37 -04:00
|
|
|
export class Inject {
|
2014-11-21 21:19:23 -08:00
|
|
|
token;
|
2014-10-03 20:34:37 -04:00
|
|
|
@CONST()
|
2014-10-07 10:34:07 -04:00
|
|
|
constructor(token) {
|
2014-10-03 20:34:37 -04:00
|
|
|
this.token = token;
|
|
|
|
}
|
2014-10-05 16:25:42 -04:00
|
|
|
}
|
|
|
|
|
2014-10-14 16:00:35 -04:00
|
|
|
/**
|
|
|
|
* A parameter annotation that creates an asynchronous eager dependency.
|
|
|
|
*
|
2014-12-04 11:01:19 +01:00
|
|
|
* ```
|
|
|
|
* class AComponent {
|
2015-04-15 22:35:38 +00:00
|
|
|
* constructor(@InjectPromise(MyService) aServicePromise:Promise<MyService>) {
|
|
|
|
* aServicePromise.then(aService:MyService => ...);
|
2014-12-04 11:01:19 +01:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2014-10-14 16:00:35 -04:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* @exportedAs angular2/di_annotations
|
2014-10-14 16:00:35 -04:00
|
|
|
*/
|
2014-10-10 15:44:56 -04:00
|
|
|
export class InjectPromise {
|
2014-11-21 21:19:23 -08:00
|
|
|
token;
|
2014-10-05 16:25:42 -04:00
|
|
|
@CONST()
|
2014-10-07 10:34:07 -04:00
|
|
|
constructor(token) {
|
2014-10-05 16:25:42 -04:00
|
|
|
this.token = token;
|
|
|
|
}
|
2014-10-06 13:45:24 -04:00
|
|
|
}
|
|
|
|
|
2014-10-14 16:00:35 -04:00
|
|
|
/**
|
|
|
|
* A parameter annotation that creates a synchronous lazy dependency.
|
|
|
|
*
|
2014-12-04 11:01:19 +01:00
|
|
|
* ```
|
|
|
|
* class AComponent {
|
2015-04-15 22:35:38 +00:00
|
|
|
* constructor(@InjectLazy(MyService) aServiceFn:Function) {
|
|
|
|
* var aService:MyService = aServiceFn();
|
2014-12-04 11:01:19 +01:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2014-10-14 16:00:35 -04:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* @exportedAs angular2/di_annotations
|
2014-10-14 16:00:35 -04:00
|
|
|
*/
|
2014-10-06 13:45:24 -04:00
|
|
|
export class InjectLazy {
|
2014-11-21 21:19:23 -08:00
|
|
|
token;
|
2014-10-06 13:45:24 -04:00
|
|
|
@CONST()
|
2014-10-07 10:34:07 -04:00
|
|
|
constructor(token) {
|
2014-10-06 13:45:24 -04:00
|
|
|
this.token = token;
|
|
|
|
}
|
2014-10-09 12:09:50 -04:00
|
|
|
}
|
2014-10-14 16:00:35 -04:00
|
|
|
|
2015-02-27 07:42:51 -08:00
|
|
|
/**
|
2015-04-15 22:35:38 +00:00
|
|
|
* A parameter annotation that marks a dependency as optional. (Injects `null` if not found.)
|
2015-02-27 07:42:51 -08:00
|
|
|
* ```
|
|
|
|
* class AComponent {
|
2015-04-15 22:35:38 +00:00
|
|
|
* constructor(@Optional() aService:MyService) {
|
|
|
|
* this.aService = aService;
|
2015-02-27 07:42:51 -08:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* @exportedAs angular2/di_annotations
|
2015-02-27 07:42:51 -08:00
|
|
|
*/
|
|
|
|
export class Optional {
|
|
|
|
@CONST()
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 16:00:35 -04:00
|
|
|
/**
|
|
|
|
* `DependencyAnnotation` is used by the framework to extend DI.
|
|
|
|
*
|
|
|
|
* Only annotations implementing `DependencyAnnotation` will be added
|
|
|
|
* to the list of dependency properties.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
2014-12-04 11:01:19 +01:00
|
|
|
* ```
|
|
|
|
* class Parent extends DependencyAnnotation {}
|
|
|
|
* class NotDependencyProperty {}
|
2014-10-14 16:00:35 -04:00
|
|
|
*
|
2014-12-04 11:01:19 +01:00
|
|
|
* class AComponent {
|
|
|
|
* constructor(@Parent @NotDependencyProperty aService:AService) {}
|
|
|
|
* }
|
|
|
|
* ```
|
2014-10-14 16:00:35 -04:00
|
|
|
*
|
|
|
|
* will create the following dependency:
|
|
|
|
*
|
2014-12-04 11:01:19 +01:00
|
|
|
* ```
|
|
|
|
* new Dependency(Key.get(AService), [new Parent()])
|
|
|
|
* ```
|
2014-10-14 16:00:35 -04:00
|
|
|
*
|
|
|
|
* The framework can use `new Parent()` to handle the `aService` dependency
|
|
|
|
* in a specific way.
|
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* @exportedAs angular2/di_annotations
|
2014-10-14 16:00:35 -04:00
|
|
|
*/
|
|
|
|
export class DependencyAnnotation {
|
|
|
|
@CONST()
|
|
|
|
constructor() {
|
|
|
|
}
|
2015-03-29 14:56:18 +02:00
|
|
|
|
|
|
|
get token() {
|
|
|
|
return null;
|
|
|
|
}
|
2015-03-16 14:43:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-15 22:35:38 +00:00
|
|
|
* A marker annotation that marks a class as available to `Injector`s for creation. Used by tooling for generating
|
|
|
|
* constructor stubs.
|
2015-03-16 14:43:22 -07:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* class NeedsService {
|
|
|
|
* constructor(svc:UsefulService) {}
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @Injectable
|
|
|
|
* class UsefulService {}
|
|
|
|
* ```
|
2015-04-15 22:35:38 +00:00
|
|
|
* @exportedAs angular2/di_annotations
|
2015-03-16 14:43:22 -07:00
|
|
|
*/
|
|
|
|
export class Injectable {
|
|
|
|
@CONST()
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
}
|