@@ -41,38 +41,37 @@ export class Dependency {
4141const _EMPTY_LIST = CONST_EXPR ( [ ] ) ;
4242
4343/**
44- * Describes how_ the {@link Injector} should instantiate a given token.
44+ * Describes how the {@link Injector} should instantiate a given token.
4545 *
4646 * See {@link bind}.
4747 *
48- * ## Example
48+ * ### Example ([live demo](http://plnkr.co/edit/GNAyj6K6PfYg2NBzgwZ5?p%3Dpreview&p=preview))
4949 *
5050 * ```javascript
5151 * var injector = Injector.resolveAndCreate([
52- * new Binding(String , { toValue: 'Hello' })
52+ * new Binding("message" , { toValue: 'Hello' })
5353 * ]);
5454 *
55- * expect(injector.get(String )).toEqual('Hello');
55+ * expect(injector.get("message" )).toEqual('Hello');
5656 * ```
5757 */
5858@CONST ( )
5959export class Binding {
6060 /**
61- * Token used when retrieving this binding. Usually the `Type`.
61+ * Token used when retrieving this binding. Usually, it is a type { @link `Type`} .
6262 */
6363 token ;
6464
6565 /**
66- * Binds an interface to an implementation / subclass .
66+ * Binds a DI token to an implementation class .
6767 *
68- * ## Example
68+ * ### Example ([live demo](http://plnkr.co/edit/RSTG86qgmoxCyj9SWPwY?p=preview))
6969 *
7070 * Because `toAlias` and `toClass` are often confused, the example contains both use cases for
7171 * easy
7272 * comparison.
7373 *
74- * ```javascript
75- *
74+ * ```typescript
7675 * class Vehicle {}
7776 *
7877 * class Car extends Vehicle {}
@@ -96,33 +95,32 @@ export class Binding {
9695 toClass : Type ;
9796
9897 /**
99- * Binds a key to a value.
98+ * Binds a DI token to a value.
10099 *
101- * ## Example
100+ * ### Example ([live demo](http://plnkr.co/edit/UFVsMVQIDe7l4waWziES?p=preview))
102101 *
103102 * ```javascript
104103 * var injector = Injector.resolveAndCreate([
105- * new Binding(String , { toValue: 'Hello' })
104+ * new Binding("message" , { toValue: 'Hello' })
106105 * ]);
107106 *
108- * expect(injector.get(String )).toEqual('Hello');
107+ * expect(injector.get("message" )).toEqual('Hello');
109108 * ```
110109 */
111110 toValue ;
112111
113112 /**
114- * Binds a key to the alias for an existing key .
113+ * Binds a DI token as an alias for an existing token .
115114 *
116115 * An alias means that {@link Injector} returns the same instance as if the alias token was used.
117116 * This is in contrast to `toClass` where a separate instance of `toClass` is returned.
118117 *
119- * ## Example
118+ * ### Example ([live demo](http://plnkr.co/edit/QsatsOJJ6P8T2fMe9gr8?p=preview))
120119 *
121120 * Because `toAlias` and `toClass` are often confused the example contains both use cases for easy
122121 * comparison.
123122 *
124- * ```javascript
125- *
123+ * ```typescript
126124 * class Vehicle {}
127125 *
128126 * class Car extends Vehicle {}
@@ -146,39 +144,43 @@ export class Binding {
146144 toAlias ;
147145
148146 /**
149- * Binds a key to a function which computes the value.
147+ * Binds a DI token to a function which computes the value.
150148 *
151- * ## Example
149+ * ### Example ([live demo](http://plnkr.co/edit/Scoxy0pJNqKGAPZY1VVC?p=preview))
152150 *
153- * ```javascript
151+ * ```typescript
154152 * var injector = Injector.resolveAndCreate([
155153 * new Binding(Number, { toFactory: () => { return 1+2; }}),
156154 * new Binding(String, { toFactory: (value) => { return "Value: " + value; },
157- * dependencies : [Number] })
155+ * deps : [Number] })
158156 * ]);
159157 *
160158 * expect(injector.get(Number)).toEqual(3);
161159 * expect(injector.get(String)).toEqual('Value: 3');
162160 * ```
161+ *
162+ * Used in conjuction with dependencies.
163163 */
164164 toFactory : Function ;
165165
166166 /**
167- * Used in conjunction with `toFactory` and specifies a set of dependencies
167+ * Specifies a set of dependencies
168168 * (as `token`s) which should be injected into the factory function.
169169 *
170- * ## Example
170+ * ### Example ([live demo](http://plnkr.co/edit/Scoxy0pJNqKGAPZY1VVC?p=preview))
171171 *
172- * ```javascript
172+ * ```typescript
173173 * var injector = Injector.resolveAndCreate([
174174 * new Binding(Number, { toFactory: () => { return 1+2; }}),
175175 * new Binding(String, { toFactory: (value) => { return "Value: " + value; },
176- * dependencies : [Number] })
176+ * deps : [Number] })
177177 * ]);
178178 *
179179 * expect(injector.get(Number)).toEqual(3);
180180 * expect(injector.get(String)).toEqual('Value: 3');
181181 * ```
182+ *
183+ * Used in conjunction with `toFactory`.
182184 */
183185 dependencies : Object [ ] ;
184186
@@ -201,12 +203,13 @@ export class Binding {
201203 this . _multi = multi ;
202204 }
203205
206+ // TODO: Provide a full working example after alpha38 is released.
204207 /**
205- * Used to create multiple bindings matching the same token.
208+ * Creates multiple bindings matching the same token (a multi-binding) .
206209 *
207- * ## Example
210+ * ### Example
208211 *
209- * ```javascript
212+ * ```typescript
210213 * var injector = Injector.resolveAndCreate([
211214 * new Binding("Strings", { toValue: "String1", multi: true}),
212215 * new Binding("Strings", { toValue: "String2", multi: true})
@@ -215,25 +218,35 @@ export class Binding {
215218 * expect(injector.get("Strings")).toEqual(["String1", "String2"]);
216219 * ```
217220 *
218- * Multi bindings and regular bindings cannot be mixed. The following
221+ * Multi- bindings and regular bindings cannot be mixed. The following
219222 * will throw an exception:
220223 *
221- * ```javascript
224+ * ```typescript
222225 * var injector = Injector.resolveAndCreate([
223- * new Binding("Strings", { toValue: "String1", multi: true}),
226+ * new Binding("Strings", { toValue: "String1", multi: true }),
224227 * new Binding("Strings", { toValue: "String2"})
225228 * ]);
226229 * ```
227230 */
228231 get multi ( ) : boolean { return normalizeBool ( this . _multi ) ; }
229232}
230233
234+
231235/**
232236 * An internal resolved representation of a {@link Binding} used by the {@link Injector}.
233237 *
234- * A {@link Binding} is resolved when it has a factory function. Binding to a class, alias, or
235- * value, are just convenience methods, as {@link Injector} only operates on calling factory
236- * functions.
238+ * It is usually created automatically by `Injector.resolveAndCreate`.
239+ *
240+ * It can be created manually, as follows:
241+ *
242+ * ### Example ([live demo](http://plnkr.co/edit/RfEnhh8kUEI0G3qsnIeT?p%3Dpreview&p=preview))
243+ *
244+ * ```typescript
245+ * var resolvedBindings = Injector.resolve([new Binding('message', {toValue: 'Hello'})]);
246+ * var injector = Injector.fromResolvedBindings(resolvedBindings);
247+ *
248+ * expect(injector.get('message')).toEqual('Hello');
249+ * ```
237250 */
238251export class ResolvedBinding {
239252 constructor (
@@ -247,10 +260,18 @@ export class ResolvedBinding {
247260 */
248261 public resolvedFactories : ResolvedFactory [ ] ,
249262
263+ /**
264+ * Indicates if the binding is a multi-binding or a regular binding.
265+ */
250266 public multiBinding : boolean ) { }
267+
268+ /** @private */
251269 get resolvedFactory ( ) : ResolvedFactory { return this . resolvedFactories [ 0 ] ; }
252270}
253271
272+ /**
273+ * An internal resolved representation of a factory function created by resolving {@link Binding}.
274+ */
254275export class ResolvedFactory {
255276 constructor (
256277 /**
@@ -265,40 +286,13 @@ export class ResolvedFactory {
265286}
266287
267288/**
268- * Provides an API for imperatively constructing {@link Binding}s .
289+ * Creates a {@link Binding}.
269290 *
270- * To construct a {@link Binding}, bind a `token` to either a class, a value or a factory function.
291+ * To construct a {@link Binding}, bind a `token` to either a class, a value, a factory function, or
292+ * to an alias to another `token`.
271293 * See {@link BindingBuilder} for more details.
272294 *
273- * The `token` is most commonly an {@link angular2/di/OpaqueToken} or a class.
274- *
275- * `bind` is only relevant for JavaScript. For Dart use the {@link Binding} constructor.
276- *
277- * ## Example
278- *
279- * ```typescript
280- * // inj.get(MyClass) would instantiate MyClass
281- * bind(MyClass).toClass(MyClass);
282- *
283- * // inj.get(MyClass) === 'my class'
284- * bind(MyClass).toValue('my class');
285- *
286- * // inj.get(MyClass) would instantiate the depenency and call the factory function with the
287- * // instance
288- * bind(MyClass).toFactory(dep => new MyClass(dep), [DepClass]);
289- *
290- * // inj.get(MyOtherClass) === inj.get(MyClass)
291- * bind(MyOtherClass).toAlias(MyClass);
292- * ```
293- *
294- * ```dart
295- * var binding = new Binding(MyClass, toClass: MyClass);
296- * var binding = new Binding(MyClass, toValue: 'my class');
297- * var binding = new Binding(MyClass, toFactory: (dep) => new MyClass(dep),
298- * dependencies: [DepClass]);
299- * var binding = new Binding(MyOtherClass, toAlias: MyClass);
300- * ```
301- *
295+ * The `token` is most commonly a class or {@link angular2/di/OpaqueToken}.
302296 */
303297export function bind ( token ) : BindingBuilder {
304298 return new BindingBuilder ( token ) ;
@@ -311,15 +305,14 @@ export class BindingBuilder {
311305 constructor ( public token ) { }
312306
313307 /**
314- * Binds an interface to an implementation / subclass .
308+ * Binds a DI token to a class .
315309 *
316- * ## Example
310+ * ### Example ([live demo](http://plnkr.co/edit/ZpBCSYqv6e2ud5KXLdxQ?p=preview))
317311 *
318312 * Because `toAlias` and `toClass` are often confused, the example contains both use cases for
319313 * easy comparison.
320314 *
321- * ```javascript
322- *
315+ * ```typescript
323316 * class Vehicle {}
324317 *
325318 * class Car extends Vehicle {}
@@ -343,34 +336,33 @@ export class BindingBuilder {
343336 toClass ( type : Type ) : Binding { return new Binding ( this . token , { toClass : type } ) ; }
344337
345338 /**
346- * Binds a key to a value.
339+ * Binds a DI token to a value.
347340 *
348- * ## Example
341+ * ### Example ([live demo](http://plnkr.co/edit/G024PFHmDL0cJFgfZK8O?p=preview))
349342 *
350- * ```javascript
343+ * ```typescript
351344 * var injector = Injector.resolveAndCreate([
352- * bind(String ).toValue('Hello')
345+ * bind('message' ).toValue('Hello')
353346 * ]);
354347 *
355- * expect(injector.get(String )).toEqual('Hello');
348+ * expect(injector.get('message' )).toEqual('Hello');
356349 * ```
357350 */
358351 toValue ( value : any ) : Binding { return new Binding ( this . token , { toValue : value } ) ; }
359352
360353 /**
361- * Binds a key to the alias for an existing key .
354+ * Binds a DI token as an alias for an existing token .
362355 *
363356 * An alias means that we will return the same instance as if the alias token was used. (This is
364357 * in contrast to `toClass` where a separate instance of `toClass` will be returned.)
365358 *
366- * ## Example
359+ * ### Example ([live demo](http://plnkr.co/edit/uBaoF2pN5cfc5AfZapNw?p=preview))
367360 *
368361 * Because `toAlias` and `toClass` are often confused, the example contains both use cases for
369362 * easy
370363 * comparison.
371364 *
372- * ```javascript
373- *
365+ * ```typescript
374366 * class Vehicle {}
375367 *
376368 * class Car extends Vehicle {}
@@ -399,11 +391,11 @@ export class BindingBuilder {
399391 }
400392
401393 /**
402- * Binds a key to a function which computes the value.
394+ * Binds a DI token to a function which computes the value.
403395 *
404- * ## Example
396+ * ### Example ([live demo](http://plnkr.co/edit/OejNIfTT3zb1iBxaIYOb?p=preview))
405397 *
406- * ```javascript
398+ * ```typescript
407399 * var injector = Injector.resolveAndCreate([
408400 * bind(Number).toFactory(() => { return 1+2; }),
409401 * bind(String).toFactory((v) => { return "Value: " + v; }, [Number])
0 commit comments