https://github.com/florianv/objar
Simple dependency injection container
https://github.com/florianv/objar
Last synced: 2 months ago
JSON representation
Simple dependency injection container
- Host: GitHub
- URL: https://github.com/florianv/objar
- Owner: florianv
- License: mit
- Created: 2014-08-04T22:46:52.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-01-03T09:41:16.000Z (over 10 years ago)
- Last Synced: 2025-04-10T18:24:41.189Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 143 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# objar [![Build status][travis-image]][travis-url]
> Simple and fast dependency injection container
## Installation
### Node
```bash
$ npm install objar --save
``````js
var objar = require('objar');
var jar = new objar();
```### Browser
```bash
$ bower install objar --save
``````html
```
```js
// Global
var jar = new objar();// RequireJS
require(['objar'], function (objar) {
var jar = new objar();
});
```## Usage
### Defining and Resolving services
```js
function Logger() {
this.log = function (message) {
console.log('Logged: ' + message);
};
}function ProductController(logger) {
this.logger = logger;this.saveProduct = function () {
this.logger.log('Saved the product!');
};
}// Defines the 'logger' service
jar.define('logger', function () {
return new Logger();
});// Defines the 'controller' service depending on the 'logger' service
jar.define('controller', function () {
return new ProductController(jar.resolve('logger'));
});// Resolving the product controller service
var controller = jar.resolve('controller');// => Logged: Saved the product!
controller.saveProduct();
```The `define()` method accepts two parameters:
- the service name
- a function returning the service (it can be any value except `undefined`)> Services are created lazily when calling the `resolve()` method and it always returns
> the same instance (singleton)### Circular dependencies
When two services depend on each other (in)directly, an Error will be thrown showing
the cycle path in order to help you to fix it.```js
// 'hello' depends on 'world'
jar.define('hello', function () {
return 'hello' + jar.resolve('world');
});// 'world' depends on 'hello'
jar.define('world', function () {
return 'world' + jar.resolve('hello');
});// Error: Circular dependency detected: hello -> world -> hello
jar.resolve('hello');
```### Invoking functions
You can invoke annotated functions depending on services (AngularJS way).
```js
jar
.define('hello', function () {
return 'hello';
})
.define('world', function () {
return 'world';
})
;// Invoke the function with the services injected:
// - w: will be the 'hello' service
// - h: will be the 'world' service
jar.invoke(['hello', 'world', function (h, w) {
// => hello world
console.log(h + ' ' + w);
}]);
```## License
[MIT](https://github.com/florianv/objar/blob/master/LICENSE)
[travis-url]: https://travis-ci.org/florianv/objar
[travis-image]: http://img.shields.io/travis/florianv/objar.svg?style=flat-square