IoC
IoC 容器类
Constructor Summary
Public Constructor | ||
public |
constructor(config: IoCConfig) 根据配置实例化一个 IoC 容器 |
Method Summary
Public Methods | ||
public |
addComponent(id: string | Object<string, ComponentConfig>, config: ComponentConfig) 向容器中注册组件 |
|
public |
addPlugins(plugins: ILifeCircleHook[], pos: number) 在指定位置添加插件 |
|
public |
dispose() 销毁容器,会遍历容器中的单例,如果有设置 dispose,调用他们的 dispose 方法 |
|
public |
getComponent(id: string | string[]): Promise<*>|Promise<*[]> 获取组件实例 |
|
public |
获取当前实例的插件队列 |
|
public |
hasComponent(id: string): bool 检测是否注册过某个组件 |
|
public |
removePlugin(pluginOrPos: number | ILifeCircleHook): bool 移除指定的插件或指定位置的插件 |
|
public |
setLoaderFunction(amdLoader: Function) 设置 IoC 的模块加载器 |
Protected Methods | ||
protected |
initConfig(iocConfig: IoCConfig) 初始化配置 |
Public Constructors
Public Methods
public addComponent(id: string | Object<string, ComponentConfig>, config: ComponentConfig) source
向容器中注册组件
Params:
Name | Type | Attribute | Description |
id | string | Object<string, ComponentConfig> | 组件 id 或者组件配置集合 |
|
config | ComponentConfig |
|
组件配置, 第一个参数为组件 id 时有效 |
Example:
ioc.addComponent('list', {
// 构造函数创建构件 new creator, 或者字符串,字符串则为 amd 模块名
creator: require('./List'),
scope: 'transient',
args: [{$ref: 'entityName'}],
// 属性注入, 不设置$setter, 则直接instance.xxx = xxx
properties: {
model: {$ref: 'listModel'},
view: {$ref: 'listView'},
name: 'xxxx' // 未设置$ref/$import操作符,'xxxx' 即为依赖值
}
});
ioc.addComponent({
listData: {
creator: 'ListData',
scope: 'transient',
properties: {
data: {
$import: 'requestStrategy', // 创建匿名组件,默认继承 requestStrategy 的配置,
args: ['list', 'list'] // 重写 requestStrategy 的 args 配置
}
}
}
});
public addPlugins(plugins: ILifeCircleHook[], pos: number) source
在指定位置添加插件
Params:
Name | Type | Attribute | Description |
plugins | ILifeCircleHook[] | 插件数组 |
|
pos | number |
|
插入位置, 默认为当前 ioc 容器插件队列末尾 |
public getComponent(id: string | string[]): Promise<*>|Promise<*[]> source
获取组件实例
Params:
Name | Type | Attribute | Description |
id | string | string[] | 单个组件 id 字符串或者一系列组件 id 数组 |
public hasComponent(id: string): bool source
检测是否注册过某个组件
Params:
Name | Type | Attribute | Description |
id | string | 组件 id |
Return:
bool |
public removePlugin(pluginOrPos: number | ILifeCircleHook): bool source
移除指定的插件或指定位置的插件
Params:
Name | Type | Attribute | Description |
pluginOrPos | number | ILifeCircleHook | 插件实例或者插件位置 |
Return:
bool | 成功移除返回 true |
public setLoaderFunction(amdLoader: Function) source
设置 IoC 的模块加载器
Params:
Name | Type | Attribute | Description |
amdLoader | Function | 符合 AMD 规范的模块加载器 |