Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(application): clean up constructor some more
  • Loading branch information
dylanfoster committed Jun 22, 2016
1 parent cbb5985 commit 3432f07
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/application.js
Expand Up @@ -43,42 +43,52 @@ class Application {
options.server = options.server || {};
options.server.middlewares = options.server.middlewares || [];

const _internalModels = new Loader({
type: "model",
path: options.database.models.dir
});
const app = options.app || restify.createServer(options.server);
const connection = options.database.connection;
const controllerLoader = new Loader({
type: "controller",
path: options.controllers.dir
});
const middlewares = _.union(DEFAULT_MIDDLEWARES, options.server.middlewares);
const modelManager = new ModelManager({ connection });

Object.keys(_internalModels.modules).forEach(model => {
modelManager.addModel(_internalModels.modules[model]);
});

Object.keys(modelManager.models).forEach(model => {
if (modelManager.models[model].associate) {
modelManager.models[model].associate(modelManager.models[model], modelManager.models);
}
this._internalModels = new Loader({
type: "model",
path: options.database.models.dir
});
this.modelManager = new ModelManager({ connection });
this._addModels();
this._associateModels();

const routerSettings = {
app,
loader: {
controllers: controllerLoader,
models: modelManager.models
models: this.modelManager.models
}
};

app.use(restify.acceptParser(app.acceptable));
middlewares.forEach(middlware => { app.use(middlware); });
this.app = app;
this.map = Router.map.bind(null, routerSettings);
this.modelManager = modelManager;
}

_addModels() {
const _internalModels = this._internalModels;

Object.keys(_internalModels.modules).forEach(model => {
this.modelManager.addModel(_internalModels.modules[model]);
});
}

_associateModels() {
const modelManager = this.modelManager;

Object.keys(modelManager.models).forEach(model => {
if (modelManager.models[model].associate) {
modelManager.models[model].associate(modelManager.models[model], modelManager.models);
}
});
}

getApp() {
Expand Down

0 comments on commit 3432f07

Please sign in to comment.