This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/app/models/BaseModel.js
2021-01-31 12:53:52 -08:00

51 lines
1.0 KiB
JavaScript

// @flow
import { set, observable } from "mobx";
export default class BaseModel {
@observable id: string;
@observable isSaving: boolean;
store: *;
constructor(fields: Object, store: *) {
set(this, fields);
this.store = store;
}
save = async (params: ?Object) => {
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 = (): Object => {
return { ...this };
};
}