-
Notifications
You must be signed in to change notification settings - Fork 1
Model
constructor(data?: object);
The model constructor can receive an previously serialised model instance
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.
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.
The static refs
object defines all references that this model type will have. You can find more details about this in the "References" guide.
The attribute that will be used as a model instance identifier. The default value is "id"
.
The attribute that will be used as a model type identifier when serialising and deserialising the data. The default value is "__type__"
.
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
.
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.
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.
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<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<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(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(): number|string
Returns the model ID
getRecordType(): number|string
Returns the model type
toJS(): IDictionary
Method that returns a serialised version of the model instance. A new model instance can be created from this value.
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(patch: IPatch): void;
Apply an an IPatch on the model.