Skip to content

Commit 1e3f908

Browse files
author
hirsch88
committed
add ioc model magic :-)
1 parent 81c4da9 commit 1e3f908

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

src/container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ ioc.configure((container: Container) => {
3232
/**
3333
* Core
3434
*/
35-
container.bind<EventEmitter>(Types.Core).toConstantValue(events).whenTargetNamed(Core.Events);
36-
container.bind<typeof Log>(Types.Core).toConstantValue(Log).whenTargetNamed(Core.Log);
35+
// container.bind<EventEmitter>(Types.Core).toConstantValue(events).whenTargetNamed(Core.Events);
36+
// container.bind<typeof Log>(Types.Core).toConstantValue(Log).whenTargetNamed(Core.Log);
3737

3838
/**
3939
* Model
4040
*/
41-
container.bind<any>(Types.Model).toConstantValue(User).whenTargetNamed(Model.User);
41+
// container.bind<any>(Types.Model).toConstantValue(User).whenTargetNamed(Model.User);
4242

4343
/**
4444
* Controllers

src/core/Bootstrap.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ export class Bootstrap {
7272

7373
private async bindIoC(): Promise<void> {
7474
log.info('Binding IoC modules...');
75-
await ioc.bindModules();
75+
try {
76+
await ioc.bindModules();
77+
} catch (error) {
78+
// TODO
79+
console.error(error);
80+
}
7681
}
7782

7883
private setupIoC(): void {

src/core/IoC.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { events, EventEmitter } from './api/events';
1616
import { Log } from './log';
1717

1818

19+
import { User } from '../api/models/User';
20+
21+
1922
class IoC {
2023

2124
public container: Container;
@@ -34,15 +37,16 @@ class IoC {
3437
}
3538

3639
public async bindModules(): Promise<void> {
37-
// this.bindCore();
38-
// this.bindModels();
40+
this.bindCore();
41+
await this.bindModels();
3942
// this.bindControllers();
4043

4144
this.container = this.customConfiguration(this.container);
4245
}
4346

4447
private bindCore(): void {
4548
this.container.bind<typeof Log>(Types.Core).toConstantValue(Log).whenTargetNamed(Core.Log);
49+
this.container.bind<EventEmitter>(Types.Core).toConstantValue(events).whenTargetNamed(Core.Events);
4650
}
4751

4852
private async bindControllers(): Promise<void> {
@@ -59,18 +63,22 @@ class IoC {
5963
});
6064
}
6165

62-
private bindModels(): void {
63-
console.log('Models');
64-
this.getFiles('/models', (files: string[]) => {
65-
files.forEach((file: any) => {
66-
const fileExport = require(`${file.path}/${file.fileName}`);
67-
this.validateExport(fileExport[file.name]);
68-
this.validateTarget(Model, file.name);
69-
console.log(Model[file.name]);
70-
this.container
71-
.bind<any>(Types.Model)
72-
.to(fileExport[file.name])
73-
.whenTargetNamed(Model[file.name]);
66+
private bindModels(): Promise<void> {
67+
return new Promise<void>((resolve, reject) => {
68+
console.log('Models');
69+
this.getFiles('/models', (files: string[]) => {
70+
files.forEach((file: any) => {
71+
const fileExport = require(`${file.path}/${file.fileName}`);
72+
this.validateExport(fileExport[file.name]);
73+
this.validateTarget(Model, file.name);
74+
75+
this.container
76+
.bind<any>(Types.Model)
77+
.toConstantValue(fileExport[file.name])
78+
.whenTargetNamed(Model[file.name]);
79+
80+
resolve();
81+
});
7482
});
7583
});
7684
}

0 commit comments

Comments
 (0)