Files
outline/app/models/BaseModel.ts
Tom Moor 15b1069bcc chore: Move to Typescript (#2783)
This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously.

closes #1282
2021-11-29 06:40:55 -08:00

56 lines
1.1 KiB
TypeScript

import { set, observable } from "mobx";
export default class BaseModel {
@observable
id: string;
@observable
isSaving: boolean;
store: any;
constructor(fields: Record<string, any>, store: any) {
set(this, fields);
this.store = store;
}
save = async (params?: Record<string, any>) => {
this.isSaving = true;
try {
// ensure that the id is passed if the document has one
if (params) params = { ...params, id: this.id };
const model = await this.store.save(params || this.toJS());
// if saving is successful set the new values on the model itself
set(this, { ...params, ...model });
return model;
} finally {
this.isSaving = false;
}
};
fetch = (options?: any) => {
return this.store.fetch(this.id, options);
};
refresh = () => {
return this.fetch({
force: true,
});
};
delete = async () => {
this.isSaving = true;
try {
return await this.store.delete(this);
} finally {
this.isSaving = false;
}
};
toJS = (): Record<string, any> => {
return { ...this };
};
}