Hoser.js
Hoser.js is a small JavaScript library (< 4kb minified) for creating observable data objects whose attributes can be lexically scoped.
API Reference
Creating a new instance
Create a new instance in hoser by wrapping an object in hoser
.
var instance = ;
get
You can retreive any value by calling #get
on a hoser instance.
var instance = ; instance;// => 'value' instance;// => { deepKey: 'deepValue' } instance;// => 'deepValue'
set
You can set any value by calling #set
on a hoser instance. This also works for arbitrary levels of nesting.
var instance = ; instance;// => true instance;// => true instance;// => 'otherValue' instance;// => 'otherDeepValue'
watch
You can observe changes in a hoser instance by calling #watch
with a callback. You cannot observe values that have not yet been defined.
var instance = ; instance; instance; instance;// => true// both callbacks will be executed
unwatch
You can remove watchers on a hoser instance by calling #unwatch
.
var instance = ;var { /* ... */ }; instance; instnace;
scope
Creating new scopes of your data can be done with #scope
. When an instance is scoped, a new child object is returned which holds a reference to all
previous ancestors. This means that if an attribute is set on a parent object, it will immediately be available to all of it's children; however, the
inverse is not true when an attribute is set on a child object; that is, the new attribute is scoped to the child.
var instance = ;var scoped = instancescope; scoped;// => 'value' instance; scoped;// => 'otherValue' scoped; instance;// => undefined
Observing changes is also determined by the scope where the attribute was created.
var instance = ;var scoped = instancescope; scoped; instance; instance;// => true// alert will appear and console will log scoped;// => true// alert will appear and console will log