Skip to content

Commit

Permalink
Merge pull request #5 from Elao/support-callbacks
Browse files Browse the repository at this point in the history
Allow callback definitions
  • Loading branch information
Thomas Jarrand authored Feb 13, 2020
2 parents 13319ae + e9f6ec0 commit 5436ee1
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 100 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

47 changes: 42 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Microscopic dependency injection container

Given the given class, you want to declare as a service:

```js
```javascript
// MyApiClient.js
export default class MyApiClient {
constructor(host, key) {
Expand All @@ -26,10 +26,12 @@ export default class MyApiClient {

Set up your container like that:

```js
```javascript
// my-container.js
import Container from '@elao/container.js';
import { Container } from '@elao/container.js';
import { createStore } from 'redux';
import MyApiClient from './MyApiClient';
import reducer from './myReducer';

const container = new Container();

Expand All @@ -38,15 +40,50 @@ container.registerParameter('api:host', 'my.api.com');
container.registerParameter('api:key', 'xxxxxxxxxxx');

// Register a service:
container.registerDefinition('api', MyApiClient, ['api:host', 'api:key']);
container.registerService('api', MyApiClient, ['api:host', 'api:key', 'store']);

// Register a callback:
container.registerCallback('store', () => createStore(reducer));

export default container;
```

Require the `api` service wherever you need it:

```js
```javascript
import container from 'my-container.js';

container.get('api').login();
```

### Requiring

With ES modules:

```javascript
import { Container } from '@elao/container.js';
````

With CommonJS modules:

```javascript
const { Container } = require('@elao/container.js');
````
In the browser:
```html
<!DOCTYPE html>
<html>
<head>
<script src="dist/container.js" async></script>
</head>
<body>
<script>
window.addEventListener('load', () => {
const { Container } = containerjs;
});
</script>
</body>
</html>
```
7 changes: 3 additions & 4 deletions dist/container.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5436ee1

Please sign in to comment.