Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New decorator injectableAll #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ constructor injection.
- [inject()](#inject)
- [injectAll()](#injectall)
- [scoped()](#scoped)
- [injectableAll()](#injectableall)
- [Container](#container)
- [Injection Token](#injection-token)
- [Providers](#providers)
Expand Down Expand Up @@ -219,6 +220,25 @@ Class decorator factory that registers the class as a scoped dependency within t
class Foo {}
```

### injectableAll()

Class decorator factory that registers the classes with the same token within
the global container
#### Usage

```typescript
@injectableAll('tags')
class Foo {}

@injectableAll('tags')
class Bar {}

@injectable()
class FooBar {
constructor(@injectAll('tags') private fooBars: any[]) {}
}
```

## Container

The general principle behind [Inversion of Control](https://en.wikipedia.org/wiki/Inversion_of_control) (IoC) containers
Expand Down
2 changes: 2 additions & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export {default as registry} from "./registry";
export {default as singleton} from "./singleton";
export {default as injectAll} from "./inject-all";
export {default as scoped} from "./scoped";
export {default as injectableAll} from "./injectable-all";

19 changes: 19 additions & 0 deletions src/decorators/injectable-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import constructor from "../types/constructor";
import {instance as globalContainer} from "../dependency-container";
import injectable from "./injectable";
import InjectionToken from "../providers/injection-token";

/**
* Class decorator factory that registers the classes with the same token within
* the global container.
*
* @return {Function} The class decorator
*/
function injectableAll<T>(token: InjectionToken<any>): (target: constructor<T>) => void {
return function(target: constructor<T>): void {
injectable()(target);
globalContainer.register(token, target)
};
}

export default injectableAll;