Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Defining models

Darko Kukovec edited this page Jun 24, 2017 · 1 revision

Note: If you're defining your own models, you also need to update the store configuration.


The models can be defined by extending the Record class. When extending the Record class, the minimal thing you should do is to define a unique type - this should be the type defined in the JSON API model:

import {Record} from 'mobx-jsonapi-store';

class Person extends Record {}
Person.type = 'person';

Other things that can be defined that are specific to the library are References with the refs property, and default values with the defaults property:

class Pet extends Record {}
Pet.type = 'pet';
Pet.refs = {owner: 'person'};
Pet.defaults = {
  age: 0,
};

In order to ensure some properties are always observables, no matter what is given in the constructor, you should define those properties in defaults. It is enough to set them to null.

If you're using TypeScript, you can define the model in the following way:

class Pet extends Record {
  static type = 'pet';
  static refs = {owner: 'person'};
  static defaults = {age: 0};
}
Clone this wiki locally