Commit ad5b144

mo khan <mo@mokhan.ca>
2016-12-06 04:59:35
wire up all commands at once.
1 parent b6938ab
app/boot/wire-up-components-into.js
@@ -1,42 +1,49 @@
 import EventAggregator from '../infrastructure/event-aggregator';
 import FetchWorkouts from '../queries/fetch-workouts';
-import LoginCommand from '../commands/login-command';
 import Registry from '../infrastructure/registry';
 import Router from '../infrastructure/router'
 
+import * as commands from '../commands';
+
 export default class WireUpComponentsInto {
   constructor(registry = new Registry()) {
     this.registry = registry;
   }
 
   run() {
-    registry.registry('eventAggregator', (container) => {
+    this.registry.register('eventAggregator', (container) => {
       return new EventAggregator();
     }).asSingleton();
 
-    registry.register('router', (container) => {
+    this.registry.register('router', (container) => {
       return new Router({
         eventAggregator: container.resolve('eventAggregator')
       });
     }).asSingleton();
-    registerCommandsInto(this.registry);
-    registerQueriesInto(this.registry);
+    this.registerCommandsInto(this.registry);
+    this.registerQueriesInto(this.registry);
     return this.registry;
   }
 
   registerCommandsInto(registry) {
-    registry.registry('command', (container) => {
-      //eventAggregator.subscribe("LOGIN", command);
-      return new LoginCommand(container.resolve('eventAggregator'));
-    }).asSingleton();
-
-    // register each command with the event aggregator
+    for (var command in commands) {
+      console.log(`registering: ${command}`);
+      registry.register('command', (container) => {
+        return new commands[command](container.resolve('eventAggregator'));
+      }).asSingleton();
+    }
+    registry.resolveAll("command").forEach((command) => {
+      command.subscribeTo(registry.resolve('eventAggregator'));
+    });
   }
 
   registerQueriesInto(registry) {
-    registry.registry('query', (container) => {
-      //eventAggregator.subscribe("FETCH_WORKOUTS", query);
+    registry.register('query', (container) => {
       return new FetchWorkouts(container.resolve('eventAggregator'));
     }).asSingleton();
+
+    registry.resolveAll("query").forEach((query) => {
+      query.subscribeTo(registry.resolve('eventAggregator'));
+    });
   }
 }
app/commands/index.js
@@ -0,0 +1,5 @@
+import LoginCommand from './login-command';
+
+export {
+  LoginCommand,
+};
app/commands/login-command.js
@@ -8,6 +8,10 @@ export default class LoginCommand {
     this.storage = storage;
   }
 
+  subscribeTo(eventAggregator) {
+    eventAggregator.subscribe('LOGIN', this);
+  }
+
   notify(event) {
     let body = { username: event.username, password: event.password };
     this.api.post(body, this.onResponse.bind(this));
app/queries/fetch-workouts.js
@@ -6,6 +6,10 @@ export default class FetchWorkouts {
     this.api = api;
   }
 
+  subscribeTo(eventAggregator) {
+    eventAggregator.subscribe('FETCH_WORKOUTS', this);
+  }
+
   notify(event) {
     this.api.get(this.onResponse.bind(this));
   }