Skip to content
avoidwork edited this page Apr 24, 2012 · 1 revision

observer

Global observer, providing publish & subscription behavior

observer is wired as a state machine, providing stateful binding which can be triggered by setting $.state.current, or by hypermedia with a custom header, with it's key set on $.state.header

$("#id").fire("customEvent");

observer.add

Method

Adds a listener to an event

@param  {Mixed}    obj   Entity or Array of Entities or $ queries
@param  {String}   event Event being fired
@param  {Function} fn    Event listener
@param  {String}   id    [Optional / Recommended] The id for the listener
@param  {String}   scope [Optional / Recommended] The id of the object or element to be set as 'this'
@param  {String}   state [Optional] The state the listener is for
@return {Mixed} Entity, Array of Entities or undefined

Prototype

Array.prototype.on				Element.prototype.on
Number.prototype.on				String.prototype.on

Example

$("#id").on("someEvent", function () {});

observer.fire

Method

Fires an event

@param  {Mixed}  obj   Entity or Array of Entities or $ queries
@param  {String} event Event being fired
@param  {Mixed}  arg   [Optional] Argument supplied to the listener
@return {Mixed} Entity, Array of Entities or undefined

Prototype

Array.prototype.fire			Element.prototype.fire
Number.prototype.fire			String.prototype.fire

Example

$("#id").fire("someEvent");

observer.fired

Property

Total events fired since load

Example

alert($.observer.fired + " events fired");

observer.list

Method

Gets the listeners for an event

@param  {Mixed}  obj   Entity or Array of Entities or $ queries
@param  {String} event Event being queried
@return {Array} Array of listeners for the event

Prototype

Array.prototype.listeners		Element.prototype.listeners
Number.prototype.listeners		String.prototype.listeners

Example

$("#id").listeners();

observer.log

Property

Boolean indicating if events are logged to the console

Example

$.observer.log = true;

observer.remove

Method

Removes an event listener, or Array of event listeners

@param  {Mixed}  obj   Entity or Array of Entities or $ queries
@param  {String} event Event being fired
@param  {String} id    [Optional] Listener id
@param  {String} state [Optional] The state the listener is for
@return {Mixed}  Entity, Array of Entities or undefined

Prototype

Array.prototype.un				Element.prototype.un
Number.prototype.un				String.prototype.un

Example

$("#id").un("someEvent");