From d05df30a947d98b3a64c46f601f1a6f54f2fb51f Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 17 Sep 2015 13:13:40 -0700 Subject: [PATCH] docs(di): add docs to forwardRef --- modules/angular2/src/core/di/forward_ref.ts | 40 ++++++++++++++------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/modules/angular2/src/core/di/forward_ref.ts b/modules/angular2/src/core/di/forward_ref.ts index 17a434b741..dff94f837b 100644 --- a/modules/angular2/src/core/di/forward_ref.ts +++ b/modules/angular2/src/core/di/forward_ref.ts @@ -5,25 +5,29 @@ export interface ForwardRefFn { (): any; } /** * 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, - * but not yet defined. + * For instance, `forwardRef` is used when the `token` which we need to refer to for the purposes of + * 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 { - * // Incorrect way to refer to a reference which is defined later. - * // This fails because `Lock` is undefined at this point. - * constructor(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) { } + * lock: Lock; + * constructor(@Inject(forwardRef(() => Lock)) lock:Lock) { + * this.lock = lock; + * } * } * - * // Only at this point the lock is defined. + * // Only at this point Lock is defined. * 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 { @@ -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} */