LoudAccessors - JavaScript event emitting attribute accessors
LoudAccessors
is a super tiny JavaScript constructor (CoffeeScript class)
implementing event emitting getter and setter. It is intended to be used as a
micromodule or as a base class to build upon.
It mixes in the eventspitter event micromodule and provides two additional instance methods:
get( attr_name )
gets the value of an attribute and emits theread:attr_name
event (where attr_name is the name of the attribute that was read)set( attr_name, attr_value )
sets the value of an attribute, and triggers thechange:attr_name
event
Additionally, it provides a class method:
attrAccessor( attr_name, [attr2_name], [...] )
on environment supportingObject.defineProperty
it defines getter and setter for the attribute(s), so that accessing it directly triggers the events.
Example (CoffeeScript)
: @set "name"name john = "John" johnget "name" # => returns "John" and triggers "read:name" johnset "name""Johnny" # => sets name to "Johnny" and triggers "change:name" johnset "name""Joe"silent: true # => sets name to "Joe" without triggering events # Or, where Object.defineProperty is supported: @attrAccessor "name" johnname # => returns "John" and triggers "read:name" john.name = "Johnny" # => sets name to "Johnny" and triggers "change:name"