angular-cn/modules/angular2/src/di/annotations_impl.ts

128 lines
2.7 KiB
TypeScript
Raw Normal View History

import {CONST, stringify} from "angular2/src/facade/lang";
/**
2015-04-17 03:29:05 -07:00
* A parameter annotation that specifies a dependency.
*
* ```
* class AComponent {
2015-04-15 22:35:38 +00:00
* constructor(@Inject(MyService) aService:MyService) {}
* }
* ```
*
2015-04-15 22:35:38 +00:00
* @exportedAs angular2/di_annotations
*/
@CONST()
export class Inject {
constructor(public token) {}
2015-06-17 10:12:06 +02:00
toString(): string { return `@Inject(${stringify(this.token)})`; }
}
/**
* A parameter annotation that specifies a `Promise` of a dependency.
*
* ```
* class AComponent {
2015-04-15 22:35:38 +00:00
* constructor(@InjectPromise(MyService) aServicePromise:Promise<MyService>) {
* aServicePromise.then(aService:MyService => ...);
* }
* }
* ```
*
2015-04-15 22:35:38 +00:00
* @exportedAs angular2/di_annotations
*/
@CONST()
2014-10-10 15:44:56 -04:00
export class InjectPromise {
constructor(public token) {}
2015-06-17 10:12:06 +02:00
toString(): string { return `@InjectPromise(${stringify(this.token)})`; }
2014-10-06 13:45:24 -04:00
}
/**
* A parameter annotation that creates a synchronous lazy dependency.
*
* ```
* class AComponent {
2015-04-15 22:35:38 +00:00
* constructor(@InjectLazy(MyService) aServiceFn:Function) {
* var aService:MyService = aServiceFn();
* }
* }
* ```
*
2015-04-15 22:35:38 +00:00
* @exportedAs angular2/di_annotations
*/
@CONST()
2014-10-06 13:45:24 -04:00
export class InjectLazy {
constructor(public token) {}
2015-06-17 10:12:06 +02:00
toString(): string { return `@InjectLazy(${stringify(this.token)})`; }
2014-10-09 12:09:50 -04:00
}
/**
* A parameter annotation that marks a dependency as optional. {@link Injector} provides `null` if
* the dependency is not found.
*
* ```
* class AComponent {
2015-04-15 22:35:38 +00:00
* constructor(@Optional() aService:MyService) {
* this.aService = aService;
* }
* }
* ```
*
2015-04-15 22:35:38 +00:00
* @exportedAs angular2/di_annotations
*/
@CONST()
export class Optional {
2015-06-17 10:12:06 +02:00
toString(): string { return `@Optional()`; }
}
/**
* `DependencyAnnotation` is used by the framework to extend DI.
*
* Only annotations implementing `DependencyAnnotation` are added to the list of dependency
* properties.
*
* For example:
*
* ```
* class Parent extends DependencyAnnotation {}
* class NotDependencyProperty {}
*
* class AComponent {
* constructor(@Parent @NotDependencyProperty aService:AService) {}
* }
* ```
*
* will create the following dependency:
*
* ```
* new Dependency(Key.get(AService), [new Parent()])
* ```
*
* 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
*/
@CONST()
export class DependencyAnnotation {
get token() { return null; }
}
/**
* A marker annotation that marks a class as available to `Injector` for creation. Used by tooling
* for generating constructor stubs.
*
* ```
* class NeedsService {
* constructor(svc:UsefulService) {}
* }
*
* @Injectable
* class UsefulService {}
* ```
2015-04-15 22:35:38 +00:00
* @exportedAs angular2/di_annotations
*/
@CONST()
export class Injectable {
}