Begin Here

KOS Model Registration

Introduction

Once a model is defined it will need to be registered with KOS Core when your application is initialized.

Model Registration consists of adding an entry to the registry that is fed into the KosCoreContextProvider. The registration will associate a model type with the class or a function that can be used to instantiate an instance of a class.

Basic Registration

The most common model registration consists of an entry that associates the model type to the class that was created earlier:

export const Registry: IKosRegistry = {
  models: {
    [DemoFactory.type]: {
      class: DemoModel,
    },
  },
  preloadModels: [],
};

Notice that the Model Factory created earlier is being used to reference the model type id. This is a best practice to keep models and registration aligned.

Model Preloading

As part of model registration it is possible to designate a list of one or more models that are to be preloaded in which case they will be instantiated and added to the model cache early so that they are available to other models.

export const Registry: IKosRegistry = {
  models: {
    [DemoFactory.type]: {
      class: DemoModel,
    },
  },
  preloadModels: [DemoFactory.type],
};

Singletons

It is possible to specify a model as being a singleton in which case only a single instance of the model will ever be created. This is useful in cases where you have a model that contains global state that can be shared across multiple models.

export const Registry: IKosRegistry = {
  models: {
    [DemoFactory.type]: {
      class: DemoModel,
      singleton: true,
    },
  },
  preloadModels: [],
};

Model Creators

In cases where models are being preloaded or if you wanted to make use of additional resources at model creation time, it is possible to register a model creator function that will serve as a factory to create a new instance of a model. This should only be needed in rare cases where the standard "options" mechanism is not appropriate.

Here is an example of a model (which we will see later on) that is registered using a creator function:

export const Registry: IKosRegistry = {
  models: {
    [DemoFactory.type]: {
      class: DemoModel,
      singleton: true,
    },
    [DependentModelFactory.type]: {
      class: DependentModel,
      create: dependentModelCreator,
      singleton: true,
    },
  },
  preloadModels: [],
};

The actual creator is a simple function that accepts the model type and ID and returns a new instance of the model:

import type { IKosDataModelCreator } from "@coca-cola/kos-ui-core";
import { DependentModel } from "./dependent-model";

export const dependentModelCreator: IKosDataModelCreator<IDependentModel> = ({
  modelTypeId,
  id,
}) => {
  const options = { id: id || modelTypeId, color: "lightblue" };
  return new DependentModel(id || modelTypeId, options);
};
Previous
Next
On this page
Java Development
Seamlessly transition from Legacy+ systems to Freestyle microdosing and advanced distributed dispense systems.
UI Development
Using KOS SDKs, integrating Consumer and Non-consumer facing UIs becomes seamless, giving you less hassle and more time to create.
Video Library
Meet some of our development team, as they lead you through the tools, features, and tips and tricks of various KOS tools.
Resources
Familiarize yourself with KOS terminology, our reference materials, and explore additional resources that complement your KOS journey.
Copyright © 2024 TCCC. All rights reserved.