observable
install
$ npm install bloody-observable
require
var observable =
api
observable.extend(options) > subclass
creates an observable subclass.
options.getDefaults
function that returns the default value for data
, will be merged with
the data. default getDefaults
returns an empty object.
observable.create([data={}]) > o
creates a new observable instance.
optionally takes a data
argument (which, if set, must be an object).
if not set, data
is a new empty object
.
o.get(key)
returns the value for key
.
o.set({key: value, key2: value2 …})
sets value
as value for key
.
o.remove(key)
removes the value for key
o.toString()
returns the JSON string of the observable data.
o.valueOf()
returns the observable data.
o.on(type, listener)
listens the the type
event and attaches listener
to it.
o.off([type[, listener]])
stops listening :
- if no argument is set : all events
- if
type
is set : alltype
events - if
type
andlistener
are set : thelistener
for thistype
o.emit(type[, data…])
fires synchronously the given type
event, passing the data…
arguments to the listeners.
o.dispatch(cb)
creates a dispatches that, for each change in the current call-stack, notifies
cb(changes)
with an object containing all new changes.
o.stopDispatch([cb])
stops dispatching to cb
if set, otherwise removes all dispatches.
events
change
: when any change occursadd
: when the change is an additionremove
: when the change is an deletion
NOTE : changes are fired by the .set
and .remove
methods.
example
var observable = var status = observable statusstatusrequest