| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { Model } from 'pinia-orm'
- /**
- * Base class for resources that can be fetched from the API
- */
- class ApiResource extends Model {
- protected static _iriEncodedFields: Record<string, ApiResource>
- protected static _idField: string
- protected static _idLess: boolean = false
- protected static _assert: object | null = {}
- public static addIriEncodedField(name: string, target: ApiResource) {
- if (!this._iriEncodedFields) {
- this._iriEncodedFields = {}
- }
- this._iriEncodedFields[name] = target
- }
- public static getIriEncodedFields() {
- return this._iriEncodedFields
- }
- public static setIdField(name: string) {
- this._idField = name
- }
- public static getIdField() {
- return this._idField
- }
- public static setAsserts(assert: object) {
- this._assert = assert
- }
- public static getAsserts() {
- return this._assert
- }
- public static setIdLess() {
- this._idLess = true
- }
- public static isIdLess(): boolean {
- return this._idLess
- }
- /**
- * Fix the 'Cannot stringify arbitrary non-POJOs' warning, meaning server can not parse the store
- *
- * @see https://github.com/vuex-orm/vuex-orm/issues/255#issuecomment-876378684
- */
- toJSON() {
- return { ...this }
- }
- /**
- * Is it a newly created entity?
- *
- * If it is, it means this entity does not exist in the data source and that it has a temporary id
- */
- public isNew(): boolean {
- return (
- !this.id || (typeof this.id === 'string' && this.id.slice(0, 3) === 'tmp')
- )
- }
- }
- export default ApiResource
|