Skip to content
secretrobotron edited this page Dec 10, 2012 · 2 revisions

#Observer (Module)

An observer/notification system.

@expose: extend

Usage:

var Observer = require('path/to/module/observer.js');

#Observer::Notification (public Class)

A Notification object is passed to subscribers when a notification occurs. It describes the notification, encompassing references to the notification origin, the name of the notification, and some data to assist. Notifications can be cancelled by calling the cancel function, and a reason can be specified to pass on to the body which issued the notification.

  • origin [ Object ]: The object which issued the notification.
  • type [ String ]: The type of notification.
  • data [ Object ]: Arbitrary data to associate with the notification.

Usage:

var n = new Notification(origin, type, data);

##Observer::Notification::cancel (public Member Function)

Cancels a notification and records a reason for doing so.

  • reason [ String ]: The reason for canceling the notification.

Usage:

n.cancel(reason);

#Observer::Observer (public Class)

Gives an object the functionality to record and notify subscribers for typed notifications (simple implementation of Observer pattern).

  • object: [ Object ]: The object to extend with Observer functionality.

Usage:

Observer.extend(object);

##Observer::Observer::notify (public Member Function)

Executes notification procedure for the given subscriber on this Observer object.

  • object [ Object ]: The object to extend with Observer functionality.

@see: Observer::__notify

Usage:

object.notify(type, data);

##Observer::Observer::subscribe (public Member Function)

Executes subscription procedure for the given subscriber on this Observer object.

  • object [ Object ]: The object to extend with Observer functionality.

@see: Observer::__subscribe

Usage:

object.subscribe(type, subscriber);

##Observer::Observer::unsubscribe (public Member Function)

Executes unsubscription procedure for the given subscriber on this Observer object.

  • object [ Object ]: The object to extend with Observer functionality.

@see: Observer::__unsubscribe

Usage:

object.unsubscribe(type, subscriber);

##Observer::__notify (private Class Function)

Calls all the subscribers of a given notification type.

  • type: [ String ]: The type of notification identifying a group of subscribers.
  • subscriber: [ Function ]: A function which will be called when notification occurs.
  • subscriberDict: [ Object ]: The group of subscribers for an object.
  • object: [ Object ]: The object issuing the notification.

Usage:

__notify(type, data, subscriberDict, object);

##Observer::__subscribe (private Class Function)

Adds a subscriber to a group of subscribers corresponding to a given notification type.

  • type: [ String ]: The type of notification that the given subscriber should receive.
  • subscriber: [ Function ]: A function which will be called when notification occurs.
  • subscriberDict: [ Object ]: The group of subscribers for an object.

Usage:

__subscribe(type, subscriber, subscriberDict);

##Observer::__unsubscribe (private Class Function)

Removes a subscriber from a group of subscribers corresponding to a given notification type.

  • type: [ String ]: The type of notification that the given subscriber was set up to receive.
  • subscriber: [ Function ]: A function which will be called when notification occurs.
  • subscriberDict: [ Object ]: The group of subscribers for an object.

Usage:

__unsubscribe(type, subscriber, subscriberDict);
Clone this wiki locally