# draba-injector2

> components injector

Latest version **1.1.0** (published 2016-05-23) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install draba-injector2
pnpm add draba-injector2
yarn add draba-injector2
bun add draba-injector2
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2016-05-23 |
| First published | 2016-05-17 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+10 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | draba1986 |
| Maintainers | draba1986 |
| Keywords | injector, test |

## Links

- npm: https://www.npmjs.com/package/draba-injector2
- Repository: https://github.com/draba1986/draba-injector2
- Homepage: https://github.com/draba1986/draba-injector2#readme
- Issues: https://github.com/draba1986/draba-injector2/issues
- npm.io page: https://npm.io/package/draba-injector2

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) 4.12.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2016-05-23
- 1.0.1 — 2016-05-18
- 1.0.0 — 2016-05-17

## README

# draba-injector2 #

模块依赖以及加载工具，用来分离应用程序中的组件,可以使组件按需加载，能提高组件的可重用性。
由于此模块能减少组件的耦合，所以编写组件的测试变得容易。

下面介绍一个简单的例子来展示此模块的用法（具体代码见test/app目录）

目录结构

    app
        |-modules
            |-modules
                |-mod1.js
                |-mod2.js
                |-mod3.js
            |-config.js
        |-app.js
        |-config.js


依赖关系：

    "mod3" 依赖 "mod1"和"mod2"

mod1.js的内容

    module.exports = Injector.define([], function () {
        return {
            message:'This is mod1.js'
        };
    });

mod2.js的内容

    module.exports = Injector.define([], function () {
        return {
            message:'This is mod2.js'
        };
    });

mod3.js的内容

    module.exports = Injector.define([
        'modules/mod1',
        'modules/mod2'
    ], createModule);

    function createModule (mod1, mod2) {
        return {
            message:'This is mod3.js',
            mod1Message:mod1.message,
            mod2Message:mod2.message
        };
    }

如何加载使用：

    var Inject = require('draba-injector2');
    var injector = new Injector([__dirname+'/test/app/modules']);
    var mod3 = injector.inject(Inject.define(['modules.mod3'], function (mod3) {
        //now mod3 has been loaded
        return mod3;
    }));

## 规则 ##


### 模块名称格式###

    /^[0-9a-z_$\-]+([/][0-9a-z_$\-]+)*$/i

## Api ##

### Constructor Injector ###

params:

    @includePaths <array> Injector的加载目录，可以提供多个文件路径
    @loadedModules <object> 预先已经加载好的模块
    @parentInjector <Injector> 父Injector

    加载器会按照路径提供的顺序对模块进行加载，如果未找到指定的模块，会让parentInjector进行加载

### Injector.define ###

params:
    @dependencies <array> 依赖的模块
    @functionInjected <function> 模块主函数

### Injector.prototype.inject ###

params:

    @target <Module|other> Module是Injector.define生成的对象所属的类

    如果是Injector.define生成的Module对象，则inject会根据Module的依赖完成加载
    如果是其他类型，inject会直接返回target

### Injector.prototype.load ###

params:

    @moduleName <string> 模块名称

    load会根据moduleName加载相应模块，如果在includePaths中未找到，则使用parentInjector加载

## 链接 ##

[express使用示例](http://github.com/draba1986/draba-injector2-example-express.git)

---
_Source: https://npm.io/package/draba-injector2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
