µModel
Tiny, generic, fully tested model.
dataoptions
data
{Object} initialize the model with some data
options
{Object}
separator
(default:/
) separator for getting/setting nested keys
API
umodel.
get key
get a key, throwing an error if a parent key is not setset key, value
set a key, lazy-creating parent keys along the way if nestedsetnx key, value
likeset
, but only if the given key has not been set yeton "event1 [event2...], :[property]", fn
callfn
withkey, [value], [previousValue]
when an event is triggeredtrigger event, key
trigger an event set with.on
Usage
Model = require 'umodel' model = foo: 'bar'#=> model modelget 'foo'#=> 'bar' modelset 'bar/baz' 'stew'#=> [Function] modelget 'bar/baz'#=> [Function] # set only if the key "tomato" is not yet set. modelsetnx 'tomato''potato'#=> "potato" # call the function `callback` when any property is read = ...modelon 'get'callback#=> undefined # call the function `callback` when `set` or `setnx` is called on `foo/bar` or any of its descendants (a more precisely specified version of the "change" event available in many mvc frameworks) modelon 'set setnx: foo/bar' ...#=> undefined # trigger `callback` by emulating a `set` event with the key `foo` (doesn't mutate the model, just triggers callbacks) modeltrigger 'set''foo'#=> undefined
Todo
- Add
.off()
method