|
@@ -1,6 +1,9 @@
|
|
|
import { BaseView } from "./base-view";
|
|
|
import { PouchService } from "../services/pouch-service";
|
|
|
import { Observable } from "rxjs/Observable";
|
|
|
+import { Observer } from "rxjs/Observer";
|
|
|
+
|
|
|
+import { EventsManager } from "../base/events/event-manager";
|
|
|
|
|
|
export abstract class BaseListView<T> extends BaseView<T>{
|
|
|
|
|
@@ -22,18 +25,40 @@ export abstract class BaseListView<T> extends BaseView<T>{
|
|
|
*
|
|
|
*/
|
|
|
initialize() {
|
|
|
- let pouch = super.getInjectable(PouchService);
|
|
|
+ EventsManager.publish("app:loading", true);
|
|
|
|
|
|
- pouch.getAll(this.getModelName()).subscribe(result => {
|
|
|
+ super.getInjectable(PouchService).getAll(this.getModelName()).subscribe(result => {
|
|
|
this.applyFilter(result.docs);
|
|
|
+ this.subcribeToEvents();
|
|
|
+
|
|
|
+ EventsManager.publish("app:loading", false);
|
|
|
}, error => {
|
|
|
console.log(error);
|
|
|
+
|
|
|
+ EventsManager.publish("app:loading", false);
|
|
|
+ EventsManager.publish("app:error", error);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private subcribeToEvents(): void {
|
|
|
+ EventsManager.subscribe("app:changed", data => {
|
|
|
+ if (data.action === "add") {
|
|
|
+ this.items.unshift(data.item);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (data.action === "update") {
|
|
|
+ this.items[this.getSelectedIndex()] = data.item;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (data.action === "delete") {
|
|
|
+ this.items.splice(this.getSelectedIndex(), 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.setSelectedIndex(-1);
|
|
|
});
|
|
|
-
|
|
|
- pouch.changes().subscribe(result => {
|
|
|
- console.log("esto es un cambio");
|
|
|
- console.log(result);
|
|
|
- }, error => console.log(error));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -86,9 +111,15 @@ export abstract class BaseListView<T> extends BaseView<T>{
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- performDelete(): Observable<any> {
|
|
|
- // return Observable.empty();
|
|
|
- return super.getInjectable(PouchService).remove(this.getSelectedItem());
|
|
|
+ performDelete(): void {
|
|
|
+ super.getInjectable(PouchService).remove(this.getSelectedItem()).subscribe(result => {
|
|
|
+ EventsManager.publish("app:changed", {
|
|
|
+ action: "delete",
|
|
|
+ details: result
|
|
|
+ });
|
|
|
+ }, error => {
|
|
|
+ EventsManager.publish("app:error", error);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|