Tiny, generic, fully tested model.


Tiny, generic, fully tested model.

new umodel [data][options]

data {Object} initialize the model with some data

options {Object}

  • separator (default: /) separator for getting/setting nested keys


  • get key get a key, throwing an error if a parent key is not set
  • set key, value set a key, lazy-creating parent keys along the way if nested
  • setnx key, value like set, but only if the given key has not been set yet
  • on "event1 [event2...], :[property]", fn call fn with key, [value], [previousValue] when an event is triggered
  • trigger event, key trigger an event set with .on
Model = require 'umodel'
model = new Model
    foo: 'bar'
#=> model 
model.get 'foo'
#=> 'bar' 
model.set 'bar/baz'(beans) ->
#=> [Function] 
model.get 'bar/baz'
#=> [Function] 
# set only if the key "tomato" is not yet set. 
model.setnx 'tomato''potato'
#=> "potato" 
# call the function `callback` when any property is read 
callback = (key, value) -> ...
model.on '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) 
model.on 'set setnx: foo/bar'(key, value, previousValue) -> ...
#=> undefined 
# trigger `callback` by emulating a `set` event with the key `foo` (doesn't mutate the model, just triggers callbacks) 
model.trigger 'set''foo'
#=> undefined 
  • Add .off() method