ApiResource.ts 722 B

123456789101112131415161718192021222324252627
  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. /**
  7. * Fix the 'Cannot stringify arbitrary non-POJOs' warning, meaning server can not parse the store
  8. *
  9. * @see https://github.com/vuex-orm/vuex-orm/issues/255#issuecomment-876378684
  10. */
  11. toJSON () {
  12. return { ...this }
  13. }
  14. /**
  15. * Is it a newly created entity?
  16. *
  17. * If it is, it means this entity does not exist in the data source and that it has a temporary id
  18. */
  19. public isNew(): boolean {
  20. return !this.id || (typeof this.id === 'string' && this.id.slice(0, 3) === 'tmp')
  21. }
  22. }
  23. export default ApiResource