Interface for models using

Kos Container Aware

decorator.

IMPORTANT: Use TypeScript interface merging to add container management properties to your model class. This provides full type safety and IntelliSense support for all injected container functionality.

Interface Merging Pattern

// 1. Declare interface merging BEFORE the class
interface MyContainerModelImpl extends KosContainerAware<ItemModel> {}

// 2. Apply decorator and implement the class
@kosModel("my-container-model")
@kosContainerAware<ItemModel>()
export class MyContainerModelImpl implements IKosDataModel {
// All container properties are now available with full type safety
}

Custom Container Property Name

When using a custom containerProperty name, use KosContainerAwareWithProp:

// For custom container property name
interface DeviceManagerImpl extends KosContainerAwareWithProp<DeviceModel, "devices"> {}

@kosContainerAware<DeviceModel>({ containerProperty: "devices" })
class DeviceManagerImpl implements IKosDataModel {
// Access via: this.devices instead of this.container
}

Injected Properties and Methods

The decorator automatically injects the following members (configurable via options):

  • container: Core KosModelContainer instance for direct access (or custom name via containerProperty)
  • getModel(id): Retrieve a model by ID from the container
  • addModel(model): Add a model to the container
  • removeModel(id): Remove a model from the container by ID (does not destroy)
  • addAll(models): Add multiple models to the container at once
  • removeAll(ids): Remove multiple models from the container by IDs (does not destroy)
  • removeAndDestroy(id): Remove a model from the container and destroy it
  • removeAndDestroyAll(ids): Remove multiple models from the container and destroy them
  • models: Readonly KosModelContainer instance (compatible with IKosModelHolder)
  • data: Reactive array of all model instances (compatible with IKosModelHolder)

All injected getters are reactive and will automatically update UI components when container contents change.

Example

// Basic usage with minimal configuration
interface DeviceContainerModelImpl extends KosContainerAware<DeviceModel> {}

@kosModel("device-container-model")
@kosContainerAware<DeviceModel>()
class DeviceContainerModelImpl implements IKosDataModel {
constructor(modelId: string, options: ContainerOptions, context: KosCreationContext) {
this.id = modelId;
// Container automatically injected with parentId: modelId
}

// Use injected methods
addDevice(device: DeviceModel): void {
this.addModel(device); // Method injected by decorator
}

// Use injected reactive getters
get deviceCount(): number {
return this.data.length; // Reactive array getter injected by decorator
}
}

Example

// Advanced usage with indexing and custom configuration
interface UserContainerModelImpl extends KosContainerAware<UserModel> {}

@kosModel("user-container-model")
@kosContainerAware<UserModel>({
containerOptions: {
indexMap: {
byRole: "role",
byDepartment: "department",
active: (user) => user.status === "active"
},
sortKey: "lastName"
}
})
class UserContainerModelImpl implements IKosDataModel {
// Access indexed data via injected container
getActiveUsers(): UserModel[] {
return this.container.getIndexByKey("active", true);
}

getUsersByRole(role: string): UserModel[] {
return this.container.getIndexByKey("byRole", role);
}
}

See

interface KosContainerAware<T> {
    container: IKosModelContainer<T>;
    getModel(id): undefined | T;
    addModel(model): void;
    removeModel(id): void;
    addAll(models): void;
    removeAll(ids): void;
    removeAndDestroy(id): Promise<void>;
    removeAndDestroyAll(ids): Promise<void>;
    models: IKosModelContainer<T>;
    data: T[];
}

Type Parameters

  • T extends IKosDataModel = IKosDataModel

    The type of models contained in the container (must extend IKosDataModel)

Methods

  • Retrieves a model from the container by its ID.

    Parameters

    • id: string

      The unique identifier of the model to retrieve

    Returns undefined | T

    The model instance if found, undefined otherwise

  • Adds a model to the container.

    Parameters

    • model: T

      The model instance to add

    Returns void

  • Removes a model from the container by its ID.

    Parameters

    • id: string

      The unique identifier of the model to remove

    Returns void

  • Adds multiple models to the container at once.

    Parameters

    • models: T[]

      Array of model instances to add

    Returns void

  • Removes multiple models from the container by their IDs.

    Parameters

    • ids: string[]

      Array of unique identifiers of the models to remove

    Returns void

  • Removes a model from the container and destroys it. Convenience method that combines removeModel() and destroyKosModel().

    Parameters

    • id: string

      The unique identifier of the model to remove and destroy

    Returns Promise<void>

  • Removes multiple models from the container and destroys them. Convenience method that combines removeAll() and destroyKosModel().

    Parameters

    • ids: string[]

      Array of unique identifiers of the models to remove and destroy

    Returns Promise<void>

Properties

container: IKosModelContainer<T>

Core container that manages the collection of models. Provides methods to add, remove, and query model instances. Automatically injected by

Kos Container Aware

decorator.

models: IKosModelContainer<T>

The container instance managing the models. Readonly property that provides access to the KosModelContainer. Compatible with IKosModelHolder interface. Updates automatically when models are added or removed.

data: T[]

Reactive array of all model instances in the container. This is the actual array of models, compatible with IKosModelHolder interface. Updates automatically when models are added or removed.