feat(model): Usa getter e setter del model se esistono

This commit is contained in:
Maicol Battistini 2022-01-29 11:20:55 +01:00
parent b82dd13983
commit eaa0808859
No known key found for this signature in database
GPG Key ID: 4FDB0F87CDB1D34A
3 changed files with 32 additions and 13 deletions

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CssDialectMappings">
<file url="PROJECT" dialect="PostCSS" />
</component>
</project>

View File

@ -4,8 +4,13 @@ import {
} from 'coloquent';
import {snakeCase} from 'lodash-es';
import {
hasGetter,
hasSetter
} from '../utils';
export interface InstantiableModel<T extends Model = Model> {
new (): (Model | T) & {[prop: string]: any};
new(): (Model | T) & {[prop: string]: any};
}
export type IModel<T extends Model = Model> = InstanceType<InstantiableModel<T>>;
@ -24,6 +29,10 @@ export abstract class Model extends BaseModel {
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
get(target, property: string, receiver): any {
if (hasGetter(target, property)) {
return Reflect.get(target, property, receiver);
}
const snakeCasedProperty = snakeCase(property);
if (snakeCasedProperty in target.getAttributes()) {
return target.getAttribute(snakeCasedProperty);
@ -32,6 +41,10 @@ export abstract class Model extends BaseModel {
return Reflect.get(target, property, receiver);
},
set(target, property: string, value) {
if (hasSetter(target, property)) {
return Reflect.set(target, property, value);
}
target.setAttribute(snakeCase(property), value);
return true;
}

View File

@ -1,4 +1,5 @@
// noinspection JSUnusedGlobalSymbols
import '@material/mwc-snackbar';
import 'mithril';
@ -7,6 +8,7 @@ import type {Vnode} from 'mithril';
import {sync as render} from 'mithril-node-render';
type GenericObject = object & {prototype: any};
/**
* Check if class/object A is the same as or a subclass of class B.
*/
@ -44,12 +46,12 @@ export async function showSnackbar(
if (closeOtherSnackbars) {
const snackbars = document.querySelectorAll('mwc-snackbar');
for (const snackbar of snackbars) {
if (snackbar.open) {
snackbar.close();
for (const snackbar1 of snackbars) {
if (snackbar1.open) {
snackbar1.close();
}
snackbar.remove();
snackbar1.remove();
}
}
@ -120,8 +122,6 @@ type ReplaceObject = Record<string, string | Vnode | number | boolean>;
* @param {Object|boolean} replace Eventuali parametri da rimpiazzare.
* Se il parametro è "true" (valore booleano), verrà ritornato il valore come stringa
* (stesso funzionamento del parametro dedicato (sotto ))
* @param {boolean} returnAsString Se impostato a "true" vien ritornata una stringa invece di
* un Vnode di Mithril
*
* @returns {string} Stringa se non contiene HTML, altrimenti Vnode
*
@ -147,3 +147,15 @@ export function __(key: string, replace: ReplaceObject = {}): string {
return translation;
}
export function getPropertyDescriptor(object: object, property: string) {
return Object.getOwnPropertyDescriptor(Object.getPrototypeOf(object), property);
}
export function hasGetter(object: object, property: string): boolean {
return getPropertyDescriptor(object, property)?.get !== undefined;
}
export function hasSetter(object: object, property: string): boolean {
return getPropertyDescriptor(object, property)?.set !== undefined;
}