immutable and type checked model layer for cerebral based on tcomb
API
Note. If you don't know how to define types with tcomb you may want to take a look at its README or the GUIDE.
model API
Example
var Controller = ;var Model = ;var t = Modelt; // the tcomb library is re-exported // define the state typevar State = t; // the initial state of the applicationvar initialState = ; // any default input you want each action to receivevar defaultInput = {}; // instantiate the controller// the argument `State` is optional, by default its value// is the initialState's constructorvar model = ; // instantiate the controllervar controller = ; controller;
state API
Note: a path can be a string or an array.
var Path = ;
get(path: Path)
state; // => 'a@domain.com'state; // => 41
set(path: Path, value: t.Any)
state;state;
unset(path: Path)
Same as set(path, null)
.
state; // it throws if email is not a t.maybe(t.String)state; // it throws if profile.age is not a t.maybe(t.Number)
concat(path: Path, value: t.Array)
state; // tags will be ['web developer', 'rock climber', 'SF reader']
push(path: Path, value: t.Any)
Same as concat(path, [value])
.
splice(path: Path, ...args)
For each item in args
call splice()
on the target with the parameters provided by the item.
state;state; // tags will be ['web developer', 'bass player']
pop(path: Path)
state; // tags will be []
shift(path: Path)
state; // tags will be []
unshift(path: Path, value: t.Any)
state; // tags will be ['rock climber', 'web developer']
merge(path: Path, value: t.Any)
state; // other will be {a: 1, b: 2}
License
The MIT License (MIT)