Skip to content
This repository was archived by the owner on Sep 14, 2023. It is now read-only.
Darko Kukovec edited this page Dec 16, 2017 · 5 revisions

constructor

constructor(data?: object);

The model constructor can receive an previously serialised model instance

static type

The static type property should be a unique identifier of this model class. This can be either a number (and therefore enum in TypeScript), or a string.

static defaults

The static defaults object defines the default properties that should exist in all model instances. You can find more details about this in the "Defining models" guide.

static refs

The static refs object defines all references that this model type will have. You can find more details about this in the "References" guide.

static idAttribute

The attribute that will be used as a model instance identifier. The default value is "id".

static typeAttribute

The attribute that will be used as a model type identifier when serialising and deserialising the data. The default value is "__type__".

static enableAutoId

Defines if the model instance ids should be automatically generated. If not defined differently (in the autoIdFunction static method), this will mean auto increment is used. The default value is true.

static autoIdFunction

autoIdFunction(): number|string

The function that will be used to generate ids when creating new model instances if there is no specific id given and enableAutoId is set to true. The default value is a function that returns a sequence of numbers starting from 1.

static preprocess

preprocess(rawData?: object): object

Helper method used to transform the input object before creating the model instance. This can be used to either rename properties (e.g. from snake case to camel case) or any other change. The default method will return the object as is.

snapshot

A current plain JS value of the model. The value is always immutable - a new instance is created on every change. The property is a computed property, so it can be observed using MobX.

assign

assign<T>(key: string, value: T): T | IModel | Array<IModel>

Method used to assign a value to a property of the model instance. Usage of this method is optional if the property already exists (normal assignment can be used instead), but is required when adding new properties or the new property won't be observable. The method will return the new value, or if a reference is being updated, it will return the referenced model or array of models.

assignRef

assignRef<T>(key: string, value: T, type?: IType): T | IModel | Array<IModel>

Method used to assign new dynamic references. The type is not required if the value is a model instance or an array of model instances. The returned value will be the referenced model or array of models.

update

update(data: IModel | any): object

Method used to do a bulk update on the model. It can receive a different model instance or a plain JS object and it will add all their properties to the existing model.

Note: The update will skip the model id property and reserved internal properties.

getRecordId

getRecordId(): number|string

Returns the model ID

getRecordType

getRecordType(): number|string

Returns the model type

toJS

toJS(): IDictionary

Method that returns a serialised version of the model instance. A new model instance can be created from this value.

patchListen

patchListen(listener: (data: IPatch, model: IModel) => void): () => void

Add a listener for changes on the model. The listener function will be called with an IPatch object

applyPatch

applyPatch(patch: IPatch): void;

Apply an an IPatch on the model.

Clone this wiki locally