react-native-models
Implementation of the models for React Native.
Features
- serialization/deserialization and saving of models in AsyncStorage;
- support of nested models;
- setters/getters for model's properties;
- verification of property types;
- filling models from the state;
- path like syntax for keys;
- serialization/deserialization of Date and RegExp objects not supported yet. Instead of it should be used strings.
Methods
constructor
: Model
Create instance of Model. Properties is a plain object:
number: "Number" string: "String" boolean: "Boolean" object: "Object" array: "Array" modelBase: "Model"
store
: Promise
Save model in Storage
. This method serialize model and all nested models. If key doesn't specified used className
property. Key support path syntax. For example:
/book/0/book/1/book/2
restore
static : Promise
Restore model from Storage
. If key doesn't specified using className
property. If store models with keys /book/0
and /book/1
, possible to restore them by /book/*
key.
remove
static : Promise
Remove value from Store
and related record in _items
record.
serialize
: string
Serialize object.
deserialize
static : Model
Deserialize object from string.
populateFromState
Fill model's properties from given state.
fromState
static : Model
Create new instance of Model
. This method check type whenever set model's property.
require
static
Bind class name with its constructor. Need for deserialization.
Examples
Properties
; // className used instead name because babel replaces him at run-time. static { return "MyModel"; } { super a: "Number" b: "String" c: "Model" // Nested model ; // Now MyModel has two members // this._a === null // this._b === null this_a = a; // this._a === 0 this_b = b; // this._b === "foo" this_c = c; // this._c === instanceOf Model // or with validation of type this; this; this; } { this; this; const a = this; // a === 1 const b = this; // b === "bar" try this; catch error return "exception"; return "no exception"; }
Store/restore
const myModel = ;myModel;myModel; MyModel;
Store/restore (path like syntax)
const myModel = 1 "My model";const anotherModel = 2 "Another model"; myModel;
Filling state
;;; Component { superprops; // Use default values of model thisstate = ; } { // Required for instancing of models objects. MyModel; MyModel; }
Serialization/deserialization
const myModel = ;const serialized = myModel;const myModel2 = MyModel;
Testing
echo '{ "presets": ["es2015"] }' > .babelrc
npm test