https://github.com/bcherny/umodel
Tiny, generic, fully tested model.
https://github.com/bcherny/umodel
Last synced: 8 months ago
JSON representation
Tiny, generic, fully tested model.
- Host: GitHub
- URL: https://github.com/bcherny/umodel
- Owner: bcherny
- License: mit
- Created: 2013-10-12T22:40:49.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2013-10-29T00:43:34.000Z (almost 12 years ago)
- Last Synced: 2024-12-17T03:03:30.394Z (10 months ago)
- Language: CoffeeScript
- Size: 258 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# µModel
[](https://travis-ci.org/eighttrackmind/umodel.png)
[](https://ci.testling.com/eighttrackmind/umodel)Tiny, generic, fully tested model.
```coffee
new umodel [data], [options]
````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 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`## Usage
```coffee
Model = require 'umodel'model = new Model
foo: 'bar'
#=> modelmodel.get 'foo'
#=> 'bar'model.set 'bar/baz', (beans) ->
'stew'
#=> [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
```## Todo
- Add `.off()` method