1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-06-05 22:09:38 +02:00

impr: 🚸 Migliorato salvataggio e ottenimento dei dati di un model

This commit is contained in:
Maicol Battistini
2022-01-10 17:07:00 +01:00
parent 33131e8d17
commit 0799395a91

View File

@@ -332,33 +332,38 @@ export class RecordsPage extends Page {
loading.show(); loading.show();
if (isFormValid(form)) { if (isFormValid(form)) {
const data = collect(getFormData(form));
// @ts-ignore // @ts-ignore
// eslint-disable-next-line new-cap // eslint-disable-next-line new-cap
const instance = new this.model() as IModel; const instance = this.rows.get(data.get('id'), new this.model() as IModel) as IModel;
if (this.customSetter) { if (this.customSetter) {
this.customSetter(instance, collect(getFormData(form))); // eslint-disable-next-line @typescript-eslint/await-thenable
await this.customSetter(instance, data);
} else { } else {
const fields = form.find('text-field, text-area, material-select'); const filtered = data
fields .filter((item: any, id: string) => this.fieldsPrecedence.includes(id));
.filter(this.fieldsPrecedence.map((value) => `#${value}`).join(', '))
.each((index, field) => { // @ts-ignore
instance[field.id] = (field as HTMLInputElement).value; (filtered.isEmpty() ? filtered : data).each((value: string, id: string) => {
}); instance[id] = value;
fields.each((index, field) => {
instance[field.id] = (field as HTMLInputElement).value;
}); });
} }
const response = await instance.save(); const response = await instance.save();
const modelId = response.getModelId();
if (modelId) {
// @ts-ignore
const newResponse = await this.model.with(this.model.relationships).find(modelId);
const model = newResponse.getData() as IModel;
const model = response.getModel();
if (model) {
const dialogElement = dialog.get(0); const dialogElement = dialog.get(0);
if (dialogElement) { if (dialogElement) {
(dialogElement as MWCDialog).close(); (dialogElement as MWCDialog).close();
} }
this.rows.put((model as Model).getId(), model);
this.rows.put(model.getId(), model);
m.redraw(); m.redraw();
await showSnackbar(__('Record salvato'), 4000); await showSnackbar(__('Record salvato'), 4000);
} }
@@ -369,7 +374,7 @@ export class RecordsPage extends Page {
}); });
} }
getModelValue(model: IModel, field: string): any { getModelValue(model: IModel, field: string, raw = false): any {
const column = this.columns[field]; const column = this.columns[field];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
let value: any = model[field]; let value: any = model[field];
@@ -379,7 +384,7 @@ export class RecordsPage extends Page {
value = column.valueModifier(model, field); value = column.valueModifier(model, field);
} }
return value; return (value || raw) ? value : '';
} }
getElementFromType(type: string) { getElementFromType(type: string) {