2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 19:09:01 +00:00

incr: set crud observable, set update listeners

This commit is contained in:
18alantom 2022-04-28 00:09:34 +05:30
parent 25dacea269
commit 9ebe35fe93
4 changed files with 31 additions and 9 deletions

View File

@ -9,9 +9,10 @@ import { DocValue, DocValueMap } from './types';
export class DocHandler {
fyo: Fyo;
models: ModelMap = {};
singles: SinglesMap = {};
docs: Observable<DocMap> = new Observable();
models: ModelMap = {};
observer: Observable<never> = 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() {

View File

@ -263,7 +263,7 @@ export class Doc extends Observable<DocValue | Doc[]> {
}
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<DocValue | Doc[]> {
}
this._notInserted = false;
this.fyo.doc.observer.trigger(`load:${this.schemaName}`, this.name);
}
async loadLinks() {
@ -627,6 +628,7 @@ export class Doc extends Observable<DocValue | Doc[]> {
}
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<DocValue | Doc[]> {
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<DocValue | Doc[]> {
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<DocValue | Doc[]> {
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) {

View File

@ -170,7 +170,12 @@ export default class Observable<T> {
_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) {

View File

@ -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));