docs(di): add docs to forwardRef

This commit is contained in:
vsavkin 2015-09-17 13:13:40 -07:00
parent e1c0b1abcb
commit d05df30a94
1 changed files with 27 additions and 13 deletions

View File

@ -5,25 +5,29 @@ export interface ForwardRefFn { (): any; }
/** /**
* Allows to refer to references which are not yet defined. * Allows to refer to references which are not yet defined.
* *
* This situation arises when the key which we need to refer to for the purposes of DI is declared, * For instance, `forwardRef` is used when the `token` which we need to refer to for the purposes of
* but not yet defined. * DI is declared,
* but not yet defined. It is also used when the `token` which we use when creating a query is not
* yet defined.
* *
* ## Example: * ### Example ([live demo](http://plnkr.co/edit/bRs0SX2OTQiJzqvjgl8P?p=preview))
* *
* ``` * ```typescript
* class Door { * class Door {
* // Incorrect way to refer to a reference which is defined later. * lock: Lock;
* // This fails because `Lock` is undefined at this point. * constructor(@Inject(forwardRef(() => Lock)) lock:Lock) {
* constructor(lock:Lock) { } * this.lock = lock;
* * }
* // Correct way to refer to a reference which is defined later.
* // The reference needs to be captured in a closure.
* constructor(@Inject(forwardRef(() => Lock)) lock:Lock) { }
* } * }
* *
* // Only at this point the lock is defined. * // Only at this point Lock is defined.
* class Lock { * class Lock {
* } * }
*
* var injector = Injector.resolveAndCreate([Door, Lock]);
* var door = injector.get(Door);
* expect(door instanceof Door).toBe(true);
* expect(door.lock instanceof Lock).toBe(true);
* ``` * ```
*/ */
export function forwardRef(forwardRefFn: ForwardRefFn): Type { export function forwardRef(forwardRefFn: ForwardRefFn): Type {
@ -33,7 +37,17 @@ export function forwardRef(forwardRefFn: ForwardRefFn): Type {
} }
/** /**
* Lazily retrieve the reference value. * Lazily retrieves the reference value from a forwardRef.
*
* Acts as the identity function when given a non-forward-ref value.
*
* ### Example ([live demo](http://plnkr.co/edit/GU72mJrk1fiodChcmiDR?p=preview))
*
* ```typescript
* var ref = forwardRef(() => "refValue");
* expect(resolveForwardRef(ref)).toEqual("refValue");
* expect(resolveForwardRef("regularValue")).toEqual("regularValue");
* ```
* *
* See: {@link forwardRef} * See: {@link forwardRef}
*/ */