ApiResource.ts 930 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import {Model} from "pinia-orm";
  2. /**
  3. * Base class for resources that can be fetched from the API
  4. */
  5. export class ApiResource extends Model {
  6. private _model: typeof ApiResource | undefined = undefined;
  7. public getModel() {
  8. return this._model
  9. }
  10. public setModel(model: typeof ApiResource ) {
  11. this._model = model
  12. }
  13. /**
  14. * Fix the 'Cannot stringify arbitrary non-POJOs' warning, meaning server can not parse the store
  15. *
  16. * @see https://github.com/vuex-orm/vuex-orm/issues/255#issuecomment-876378684
  17. */
  18. toJSON () {
  19. return { ...this }
  20. }
  21. /**
  22. * Is it a newly created entity?
  23. *
  24. * If it is, it means this entity does not exist in the data source and that it has a temporary id
  25. */
  26. public isNew(): boolean {
  27. return !this.id || (typeof this.id === 'string' && this.id.slice(0, 3) === 'tmp')
  28. }
  29. }
  30. export default ApiResource