From 9ebe35fe9399ae966d70b27e5acf219d00407b41 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 28 Apr 2022 00:09:34 +0530 Subject: [PATCH] incr: set crud observable, set update listeners --- fyo/core/docHandler.ts | 4 +++- fyo/model/doc.ts | 8 +++++++- fyo/utils/observable.ts | 7 ++++++- src/pages/ListView/List.vue | 21 +++++++++++++++------ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/fyo/core/docHandler.ts b/fyo/core/docHandler.ts index c873af2e..43b046d5 100644 --- a/fyo/core/docHandler.ts +++ b/fyo/core/docHandler.ts @@ -9,9 +9,10 @@ import { DocValue, DocValueMap } from './types'; export class DocHandler { fyo: Fyo; + models: ModelMap = {}; singles: SinglesMap = {}; docs: Observable = new Observable(); - models: ModelMap = {}; + observer: Observable = new Observable(); constructor(fyo: Fyo) { this.fyo = fyo; @@ -21,6 +22,7 @@ export class DocHandler { this.models = {}; this.singles = {}; this.docs = new Observable(); + this.observer = new Observable(); } purgeCache() { diff --git a/fyo/model/doc.ts b/fyo/model/doc.ts index a1e4f8e2..07e74a63 100644 --- a/fyo/model/doc.ts +++ b/fyo/model/doc.ts @@ -263,7 +263,7 @@ export class Doc extends Observable { } const targetField = this.fieldMap[fieldname] as TargetField; - const schema = this.fyo.db.schemaMap[targetField.target] as Schema; + const schema = this.fyo.schemaMap[targetField.target] as Schema; const childDoc = new Doc(schema, data as DocValueMap, this.fyo); return childDoc; } @@ -390,6 +390,7 @@ export class Doc extends Observable { } this._notInserted = false; + this.fyo.doc.observer.trigger(`load:${this.schemaName}`, this.name); } async loadLinks() { @@ -627,6 +628,7 @@ export class Doc extends Observable { } this._notInserted = false; await this.trigger('afterSync'); + this.fyo.doc.observer.trigger(`sync:${this.schemaName}`, this.name); return doc; } @@ -636,6 +638,7 @@ export class Doc extends Observable { await this.trigger('afterDelete'); this.fyo.telemetry.log(Verb.Deleted, this.schemaName); + this.fyo.doc.observer.trigger(`delete:${this.schemaName}`, this.name); } async _submitOrRevert(isSubmit: boolean) { @@ -652,10 +655,12 @@ export class Doc extends Observable { async submit() { this.cancelled = false; await this._submitOrRevert(true); + this.fyo.doc.observer.trigger(`submit:${this.schemaName}`, this.name); } async revert() { await this._submitOrRevert(false); + this.fyo.doc.observer.trigger(`revert:${this.schemaName}`, this.name); } async rename(newName: string) { @@ -663,6 +668,7 @@ export class Doc extends Observable { await this.fyo.db.rename(this.schemaName, this.name!, newName); this.name = newName; await this.trigger('afterRename'); + this.fyo.doc.observer.trigger(`rename:${this.schemaName}`, this.name); } async trigger(event: string, params?: unknown) { diff --git a/fyo/utils/observable.ts b/fyo/utils/observable.ts index 2dba8abc..8f896d66 100644 --- a/fyo/utils/observable.ts +++ b/fyo/utils/observable.ts @@ -170,7 +170,12 @@ export default class Observable { _addListener(type: EventType, event: string, listener: Function) { this._initLiseners(type, event); - this[type].get(event)!.push(listener); + const list = this[type].get(event)!; + if (list.includes(listener)) { + return; + } + + list.push(listener); } _initLiseners(type: EventType, event: string) { diff --git a/src/pages/ListView/List.vue b/src/pages/ListView/List.vue index 1124b50b..edf5daac 100644 --- a/src/pages/ListView/List.vue +++ b/src/pages/ListView/List.vue @@ -107,14 +107,23 @@ export default { }, async mounted() { await this.updateData(); - /* - TODO: need to set callback incase that schema has data changes - fyo.db.on(`change:${this.schemaName}`, () => { - this.updateData(); - }); - */ + this.setUpdateListeners(); }, methods: { + setUpdateListeners() { + const listener = () => { + this.updateData(); + }; + + if (fyo.schemaMap[this.schemaName].isSubmittable) { + fyo.doc.observer.on(`submit:${this.schemaName}`, listener); + fyo.doc.observer.on(`revert:${this.schemaName}`, listener); + } + + fyo.doc.observer.on(`sync:${this.schemaName}`, listener); + fyo.doc.observer.on(`delete:${this.schemaName}`, listener); + fyo.doc.observer.on(`rename:${this.schemaName}`, listener); + }, openForm(doc) { if (this.listConfig.formRoute) { routeTo(this.listConfig.formRoute(doc.name));