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
This commit is contained in:
55
app/models/BaseModel.ts
Normal file
55
app/models/BaseModel.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
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 };
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user