angular-cn/public/docs/js/latest/api/di/BindingBuilder-class.jade

217 lines
4.4 KiB
Plaintext
Raw Normal View History

2015-04-19 13:53:18 -07:00
p.
<span class="location-badge">exported from <a href="/angular2/di">angular2/di</a></span>
defined in <a href="https://github.com/angular/angular/tree/master/modules/angular2/src/di/binding.js#L299">angular2/src/di/binding.js (line 299)</a>
:markdown
Helper class for [bind] function.
.l-main-section
h2 Members
.l-sub-section
h3 constructor
pre.prettyprint
code.
(token)
:markdown
.l-sub-section
h3 toAlias
pre.prettyprint
code.
(aliasToken)
:markdown
Bind a key to an alias of an existing key.
An alias means that we will return the same instance as if the alias token was used. (This is in contrast to
`toClass` where a separet instance of `toClass` will be returned.)
Becuse `toAlias` and `toClass` are often confused the example contains both use cases for easy comparison.
```javascript
class Vehicle {}
class Car extends Vehicle {}
var injectorAlias = Injector.resolveAndCreate([
Car,
bind(Vehicle).toAlias(Car)
]);
var injectorClass = Injector.resolveAndCreate([
Car,
bind(Vehicle).toClass(Car)
]);
expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
```
.l-sub-section
h3 toAsyncFactory
pre.prettyprint
code.
(factoryFunction:Function, dependencies:List = null)
:markdown
Bind a key to a function which computes the value asynchronously.
```javascript
var injector = Injector.resolveAndCreate([
bind(Number).toAsyncFactory(() => {
return new Promise((resolve) => resolve(1 + 2));
}),
bind(String).toFactory((v) => { return "Value: " + v; }, [String])
]);
injector.asyncGet(Number).then((v) => expect(v).toBe(3));
injector.asyncGet(String).then((v) => expect(v).toBe('Value: 3'));
```
The interesting thing to note is that event thougt `Numeber` has an async factory, the `String` factory
function takes the resolved value. This shows that the [Injector] delays executing of the `String` factory
until after the `Number` is resolved. This can only be done if the `token` is retrive
.l-sub-section
h3 toClass
pre.prettyprint
code.
(type:Type)
:markdown
Bind an interface to an implementation / subclass.
Becuse `toAlias` and `toClass` are often confused the example contains both use cases for easy comparison.
```javascript
class Vehicle {}
class Car extends Vehicle {}
var injectorClass = Injector.resolveAndCreate([
Car,
bind(Vehicle).toClass(Car)
]);
var injectorAlias = Injector.resolveAndCreate([
Car,
bind(Vehicle).toAlias(Car)
]);
expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
```
.l-sub-section
h3 toFactory
pre.prettyprint
code.
(factoryFunction:Function, dependencies:List = null)
:markdown
Bind a key to a function which computes the value.
```javascript
var injector = Injector.resolveAndCreate([
bind(Number).toFactory(() => { return 1+2; }}),
bind(String).toFactory((v) => { return "Value: " + v; }, [String] })
]);
expect(injector.get(Number)).toEqual(3);
expect(injector.get(String)).toEqual('Value: 3');
```
.l-sub-section
h3 toValue
pre.prettyprint
code.
(value)
:markdown
Bind a key to a value.
```javascript
var injector = Injector.resolveAndCreate([
bind(String).toValue('Hello')
]);
expect(injector.get(String)).toEqual('Hello');
```
.l-sub-section
h3 token
:markdown