Creating different Observables for models inside store

Change-Id: Ic2b190cddf04dde25a9ca7ae9445e0e9d8da42f1
diff --git a/src/app/datasources/stores/model.store.spec.ts b/src/app/datasources/stores/model.store.spec.ts
index 797685b..a484003 100644
--- a/src/app/datasources/stores/model.store.spec.ts
+++ b/src/app/datasources/stores/model.store.spec.ts
@@ -57,7 +57,7 @@
     WebSocket = _WebSocket_;
 
     // ModelRest will call the backend
-    httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/samples`)
+    httpBackend.whenGET(`${AppConfig.apiEndpoint}/core/samples`)
       .respond(queryData);
   }));
 
@@ -114,4 +114,39 @@
       httpBackend.flush();
     });
   });
+
+  describe('when multiple stores are requested', () => {
+
+    beforeEach(() => {
+      httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/firsts`)
+        .respond([
+          {first: 'foo'}
+        ]);
+      httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/seconds`)
+        .respond([
+          {second: 'foo'}
+        ]);
+    });
+    it('should create different Subject', (done) => {
+      let fevent = 0;
+      let sevent = 0;
+      service.query('first')
+        .subscribe(first => {
+          fevent++;
+          if (fevent >= 2) {
+            service.query('second')
+              .subscribe(second => {
+                sevent++;
+                if (sevent === 2) {
+                  console.log(first, second);
+                  expect(first).not.toEqual(second);
+                  done();
+                }
+              });
+          }
+        });
+      $scope.$apply();
+      httpBackend.flush();
+    });
+  });
 });
diff --git a/src/app/datasources/stores/model.store.ts b/src/app/datasources/stores/model.store.ts
index 0839cc7..903bd46 100644
--- a/src/app/datasources/stores/model.store.ts
+++ b/src/app/datasources/stores/model.store.ts
@@ -11,27 +11,33 @@
 
 export class ModelStore {
   static $inject = ['WebSocket', 'StoreHelpers', 'ModelRest'];
-  private _collection: BehaviorSubject<any[]> = new BehaviorSubject([]);
+  private _collections: any; // NOTE contains a map of {model: BehaviourSubject}
   constructor(
     private webSocket: IWSEventService,
     private storeHelpers: IStoreHelpersService,
     private ModelRest: IXosResourceService
   ) {
+    this._collections = {};
   }
-  // FIXME if called multiple time (for different collection),
-  // it return the same observable while it should return different ones
 
   query(model: string) {
-    this.loadInitialData(model);
+
+    // if there isn't already an observable for that item
+    if (!this._collections[model]) {
+      this._collections[model] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource
+      this.loadInitialData(model);
+    }
+
     this.webSocket.list()
       .filter((e: IWSEvent) => e.model === model)
       .subscribe(
         (event: IWSEvent) => {
-          this.storeHelpers.updateCollection(event, this._collection);
+          this.storeHelpers.updateCollection(event, this._collections[model]);
         },
         err => console.error
       );
-    return this._collection.asObservable();
+
+    return this._collections[model].asObservable();
   }
 
   private loadInitialData(model: string) {
@@ -40,7 +46,7 @@
     this.ModelRest.getResource(endpoint).query().$promise
       .then(
         res => {
-          this._collection.next(res);
+          this._collections[model].next(res);
         },
         err => console.log(`Error retrieving ${model}`, err)
       );
diff --git a/src/app/views/dashboard/dashboard.ts b/src/app/views/dashboard/dashboard.ts
index 776acf6..dc4fc1d 100644
--- a/src/app/views/dashboard/dashboard.ts
+++ b/src/app/views/dashboard/dashboard.ts
@@ -17,24 +17,15 @@
       this.$state.go('login');
     }
     else {
-      // this.store.query('node')
-      //   .subscribe((event) => {
-      //     console.log(`node`, event);
-      //     this.nodes = event.length;
-      // });
+      this.store.query('node')
+        .subscribe((event) => this.nodes = event.length);
       this.store.query('slice')
-        .subscribe((event) => {
-          // console.log('slice', event);
-          this.slices = event.length;
-      });
-      // this.store.query('instance')
-      //   .subscribe((event) => {
-      //     console.log('isntance', event);
-      //     this.instances = event.length;
-      // });
+        .subscribe((event) => this.slices = event.length);
+      this.store.query('instance')
+        .subscribe((event) => this.instances = event.length);
       this.instances = 0;
-      this.nodes = 2;
-      this.slices = 3;
+      this.nodes = 0;
+      this.slices = 0;
     }
   }
 }