Constructor
new DI(descriptoropt)
Use an instance if changes should be kept encapsulated and only accessible by that instance.
Its best use case is when temporary projection (See DI.setProjection) are needed. In any other case simply use DI directly.
The values of the descriptor are used as defaults anywhere in the library where descriptors are
required (e.g. DI.get)
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
descriptor |
object |
<optional> |
Descriptor used as the base for all other descriptors Properties
|
Members
(static, readonly) ACTIONS :number
List of actions applicable on the entities when requested (DI.get). For example, if the entity is a class, you probably want to return an instance of that class, so use DI.ACTIONS.CREATE.
Type:
- number
Properties:
| Name | Type | Description |
|---|---|---|
CREATE |
number | Create an instance using |
INVOKE |
number | Execute the function |
NONE |
number | Do nothing (Default if the entity is an object) |
Example
class Foo {}
DI.set({
name: 'foo',
ref: Foo,
action: DI.ACTIONS.CREATE
});
function Store() { ... }
DI.set({
name: 'app',
ref: Store,
action: DI.ACTIONS.INVOKE
});
const App = {};
DI.set({
name: 'App',
ref: App,
action: DI.ACTIONS.NONE
});
(static, readonly) DIRECTIONS :number
Lookup direction for namespace traversal.
Type:
- number
Properties:
| Name | Type | Description |
|---|---|---|
PARENT_TO_CHILD |
number | Upwards (Capturing) |
CHILD_TO_PARENT |
number | Downwards (Bubbling) |
Example
DI.set({
name: 'a.b.c.d.Foo',
lookup: DI.DIRECTIONS.CHILD_TO_PARENT
...
});
}
Methods
(static) get(name, configopt) → {*}
Returns the processed entity using of DI.ACTIONS. Use config to overwrite one or more descriptor values. Below is an example in which params is replaced.
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name |
string | name of the descriptor |
|||||||||||||
config |
object |
<optional> |
configuration Properties
|
Returns:
The processed entity (See DI.ACTIONS)
- Type
- *
Example
class Foo {
constructor(base) { this.base = base; }
addToBase(num) { return this.base + num; }
}
const descriptor = {
name: 'foo',
ref: Foo,
action: DI.ACTIONS.CREATE,
params: [100]
};
di.set(descriptor);
di.get('foo').addToBase(1); // --> 101
di.get('foo', {params: [1]}).addToBase(1); // --> 2
(static) getDescriptor(name) → {object}
Returns the descriptor identified by the given name. However, it will not traverse the namespace
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string | Entity name |
Returns:
descriptor
- Type
- object
(static) getFactory(name, config) → {function}
Returns a factory for the given entity name
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string | Entity name |
config |
object | Descriptor defaults (Checkout DI#constructor for all descriptor properties) |
Returns:
- Type
- function
Example
class Foo {
constructor(val) { this.val = val; }
}
DI.set({name: 'foo', ref: Foo, params: [1]});
const factory = DI.getFactory();
let foo = factory(); // foo.val === 1
foo = factory(2); // foo.val === 2
DI.getFactory(10)().val === 10
DI.getFactory(10)(20).val === 20
(static) getProjection(name, nsopt) → {object}
Returns the projection identified by the given name. However, it will not traverse the namespace
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
name |
string | entity name (it can include the namespace) |
|
ns |
string |
<optional> |
namespace |
Returns:
descriptor
- Type
- object
(static) lookupDescriptor(name, configopt) → {object}
This function is identical to DI.getDescriptor except it will traverse the namespace until it finds it or reaches the end of the namespace.
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
name |
|||||||||||
config |
object |
<optional> |
Configuration of the lookup process Properties
|
Returns:
Descriptor
- Type
- object
Example
const descriptor = {
name: 'a.b.foo',
...
}
DI.lookupDescriptor('a.b.c.d.e.foo', {lookup: DI.DIRECTIONS.PARENT_TO_CHILD}); // will find `a.b.foo`
(static) removeDescriptor(name) → {function}
Remove a descriptor/entity
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string | entity name |
Returns:
DI class
- Type
- function
Example
DI.set({name: 'foo', ref: Foo});
DI.removeDescriptor('foo');
DI.get('foo')
(static) removeProjection(key, projections) → {DI}
Remove one projection
Parameters:
| Name | Type | Description |
|---|---|---|
key |
||
projections |
Returns:
- Type
- DI
Example
DI.setProjection({foo: 'bar'}); // --> foo is projected to bar
DI.removeProjection('foo'); // --> remove the above projection
(static) set(descriptor) → {function}
Register an entity
Parameters:
| Name | Type | Description |
|---|---|---|
descriptor |
descriptor | defaults (Checkout DI.constructor for all descriptor properties) |
Returns:
DI class
- Type
- function
(static) setProjection(list) → {DI}
Define one or projection
Parameters:
| Name | Type | Description |
|---|---|---|
list |
Object | Projections with the key being the entity name to be replaced by its value |
Returns:
- Type
- DI
Example
DI.setProjection( { foo: 'bar', baz: 'a.b.mooz'} ); // 'foo' being projected to 'bar'
DI.get('foo') instanceof Bar
get()
SeeDI.get
getDescriptor()
See DI.getDescriptor
getFactory()
See DI.getFactory
getProjection()
See DI.getProjection
lookupDescriptor()
removeDescriptor() → {object}
Returns:
DI instance
- Type
- object
removeProjection() → {object}
Returns:
DI instance
- Type
- object
set() → {object}
See DI.set
Returns:
DI instance
- Type
- object
setProjection() → {object}
See DI.seProjection
Returns:
DI instance
- Type
- object