Class BaseModel

Represents a base model with common functionality.

Hierarchy (view full)

Indexable

[key: string]: any

Constructors

Properties

_listeners: Listeners = {}

Event listeners registered for this instance.

_uid: string = ...

Unique identifier for the instance.

errors: AbstractObject<String> = {}

The errors associated with the model.

initialFields: string[] = []

The initial fields of the model.

loading: boolean = false

Indicates whether the class is currently loading.

Accessors

  • get $class(): string
  • Gets the class name of this instance.

    Returns string

  • get isValid(): boolean
  • Checks if the model is valid.

    Returns boolean

    • True if the model is valid, false otherwise.

Methods

  • Returns a map of API functions with default values.

    Returns Endpoints

    An object containing API functions.

  • Boot method to be overridden by subclasses.

    Returns void

  • Initiates a bulk create or update operation using provided payload.

    Parameters

    • payload: any = null

      The payload for the bulk create or update operation.

    Returns Promise<any>

    A Promise representing the bulk create or update operation.

  • Initiates a bulk delete operation using provided payload.

    Parameters

    • payload: any = null

      The payload for the bulk delete operation.

    Returns Promise<any>

    A Promise representing the bulk delete operation.

  • Clears the model.

    Parameters

    • data: any = null

      Optional data to clear the model with.

    Returns void

  • Clears a specific error in the model's error object.

    Parameters

    • errorId: string

      The ID of the error to clear.

    Returns void

  • Clears multiple errors in the model's error object.

    Parameters

    • Optional errorIds: string[] = []

      An array of error IDs to clear. If empty, clears all errors.

    Returns void

  • Clears the state of the model.

    Returns void

  • Creates a shallow clone of the model.

    Returns this

    • The cloned instance of the model.
  • Initiates a create operation using provided payload.

    Parameters

    • payload: any = null

      The payload for the create operation.

    Returns Promise<any>

    A Promise representing the create operation.

  • Default state method to be overridden by subclasses.

    Returns any

    • The default state for the model.
  • Initiates a delete operation using provided payload.

    Parameters

    • payload: any = null

      The payload for the delete operation.

    Returns Promise<any>

    A Promise representing the delete operation.

  • Executes a function with loading indication.

    Parameters

    • func: Function

      The function to execute.

    • Rest ...args: any[]

      The arguments to pass to the function.

    Returns Promise<any>

    • A promise resolving to the result of the function or an error.
  • Emits an event by name to all registered listeners on that event. Listeners will be called in the order that they were added. If a listener returns false, no other listeners will be called.

    Parameters

    • event: any

      The name of the event to emit.

    • Rest ...args: any[]

      The context of the event, passed to listeners.

    Returns void

  • Initiates a fetch operation using provided payload.

    Parameters

    • payload: any

      The payload for the fetch operation.

    Returns Promise<any>

    A Promise representing the fetch operation.

  • Retrieves a property from the model.

    Parameters

    • prop: string

      The property to retrieve.

    • defaultValue: any

      The default value if the property is not found.

    Returns any

    • The value of the property.
  • Initializes the BaseModel with the provided data.

    Parameters

    • data: any

      The data to initialize the model with.

    Returns this

    • The initialized model.
  • Makes a request using the provided request function and payload.

    Parameters

    • payload: any

      The payload for the request.

    • reqFunc: any

      The request function to execute.

    Returns Promise<any>

    A Promise representing the request operation.

  • Registers an event listener for a given event.

    Event names can be comma-separated to register multiple events.

    Parameters

    • event: string

      The name of the event to listen for.

    • listener: Function

      The event listener, accepts context.

    Returns void

  • Initiates a read operation using provided payload.

    Parameters

    • payload: any = null

      The payload for the read operation.

    Returns Promise<any>

    A Promise representing the read operation.

  • Resets the model.

    Parameters

    • data: any = null

      Optional data to reset the model with.

    Returns void

  • Sets data to the model.

    Parameters

    • data: undefined | AbstractObject<any>

      The data to set to the model.

    Returns BaseModel

    • The modified model instance.
  • Initiates an update operation using provided payload.

    Parameters

    • payload: any = null

      The payload for the update operation.

    Returns Promise<any>

    A Promise representing the update operation.

  • Validates the model based on the validation object.

    Parameters

    • context: any = null

      The context for validation.

    Returns boolean

    • True if the model passes validation, false otherwise.
  • Validates and creates data on the server.

    Parameters

    • payload: any

      The payload for the create request.

    • context: any

      The context for validation.

    Returns Promise<any>

    • A promise resolving to the created data or false if validation fails.
  • Validates and deletes data on the server.

    Parameters

    • payload: any

      The payload for the delete request.

    • context: any

      The context for validation.

    Returns Promise<any>

    • A promise resolving to true if deletion is successful, false otherwise.
  • Validates and fetches data from the server.

    Parameters

    • payload: any

      The payload for the fetch request.

    • context: any

      The context for validation.

    Returns Promise<any>

    • A promise resolving to the fetched data or false if validation fails.
  • Validates the model and makes a request if validation passes.

    Parameters

    • payload: any

      The payload for the request.

    • context: any

      The context for validation.

    • requestFunc: Endpoint

      The request function.

    Returns Promise<any>

    • A promise resolving to the response of the request or false if validation fails.
  • Validates and reads data from the server.

    Parameters

    • payload: any

      The payload for the read request.

    • context: any

      The context for validation.

    Returns Promise<any>

    • A promise resolving to the read data or false if validation fails.
  • Validates and updates data on the server.

    Parameters

    • payload: any

      The payload for the update request.

    • context: any

      The context for validation.

    Returns Promise<any>

    • A promise resolving to the updated data or false if validation fails.
  • Retrieves the validation object.

    Parameters

    • _context: any = null

      The context for validation (unused).

    Returns AbstractObject<(() => string)>

    • The validation object mapping validation functions and messages.

    Example

    // Validation object is an object that should be written in such way:
    // validation passes if the property function return empty string
    // prop is a name of your class properties that should be validated
    {
    prop: () => {
    if (someCondition) {
    return 'error message';
    }
    return '';
    }
    }

Generated using TypeDoc