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

feat: Aggiunti altri tipi e interfacce per i Model

This commit is contained in:
Maicol Battistini
2022-01-07 14:22:14 +01:00
parent 5e4069930e
commit ed7c801309
3 changed files with 26 additions and 11 deletions

View File

@@ -12,7 +12,11 @@ import type {
VnodeDOM VnodeDOM
} from 'mithril'; } from 'mithril';
import {Model} from '../../Models'; import {
IModel,
IndexedModel,
Model
} from '../../Models';
import type { import type {
FieldT, FieldT,
SelectT, SelectT,
@@ -32,7 +36,7 @@ export type ColumnT = {
id?: string id?: string
title: string title: string
type?: 'checkbox' | 'numeric' type?: 'checkbox' | 'numeric'
valueModifier?: (instance: Model, property: string) => any valueModifier?: (instance: IModel<IndexedModel>, property: string) => any
}; };
export type SectionT = { export type SectionT = {
id?: string id?: string
@@ -45,11 +49,9 @@ export type SectionT = {
| Record<string, TextFieldT | TextAreaT | SelectT> | Record<string, TextFieldT | TextAreaT | SelectT>
}; };
export type ColumnsT = Record<string, string | ColumnT>; export type ColumnsT = Record<string, string | ColumnT>;
export type RowsT = Collection<Model>; export type RowsT = Collection<IModel<IndexedModel>>;
export type SectionsT = Record<string, SectionT> | SectionT[]; export type SectionsT = Record<string, SectionT> | SectionT[];
export type IndexedModel = Model & {[prop: string]: any};
/** /**
* @abstract * @abstract
*/ */
@@ -60,7 +62,10 @@ export class RecordsPage extends Page {
dialogs: Children[]; dialogs: Children[];
recordDialogMaxWidth: string | number = 'auto'; recordDialogMaxWidth: string | number = 'auto';
model: typeof Model; model: typeof Model;
customSetter: (model: Model, fields: Collection<File | string>) => void; customSetter: (
model: IModel<IndexedModel>,
fields: Collection<File | string>
) => void;
/** /**
* What fields should take precedence when saving the record * What fields should take precedence when saving the record
@@ -128,7 +133,7 @@ export class RecordsPage extends Page {
} }
return this.rows return this.rows
.map((instance: IndexedModel, index: string) => ( .map((instance: IModel<IndexedModel>, index: string) => (
<TableRow <TableRow
key={index} key={index}
data-model-id={instance.getId()} data-model-id={instance.getId()}
@@ -149,7 +154,7 @@ export class RecordsPage extends Page {
async updateRecord(id: number) { async updateRecord(id: number) {
// @ts-ignore // @ts-ignore
const response = await this.model.find(id); const response = await this.model.find(id);
const instance = response.getData() as IndexedModel; const instance = response.getData() as IModel<IndexedModel>;
const dialog = $('mwc-dialog#add-record-dialog'); const dialog = $('mwc-dialog#add-record-dialog');
dialog dialog
@@ -329,7 +334,7 @@ export class RecordsPage extends Page {
if (isFormValid(form)) { if (isFormValid(form)) {
// @ts-ignore // @ts-ignore
// eslint-disable-next-line new-cap // eslint-disable-next-line new-cap
const instance = new this.model() as IndexedModel; const instance = new this.model() as IModel<IndexedModel>;
if (this.customSetter) { if (this.customSetter) {
this.customSetter(instance, collect(getFormData(form))); this.customSetter(instance, collect(getFormData(form)));

View File

@@ -4,10 +4,20 @@ import {
} from 'coloquent'; } from 'coloquent';
import {snakeCase} from 'lodash'; import {snakeCase} from 'lodash';
export interface IndexedModel extends Model {
[prop: string]: any;
}
export interface InstantiableModel<T extends IndexedModel> {
new (): T;
}
export type IModel<T extends IndexedModel> = InstanceType<InstantiableModel<T>>;
/** /**
* The base model for all models. * The base model for all models.
*/ */
export default abstract class Model extends BaseModel { export abstract class Model extends BaseModel {
jsonApiType: string = ''; jsonApiType: string = '';
constructor() { constructor() {

View File

@@ -1 +1 @@
export {default as Model} from './Model'; export * from './Model';