Manager Class

check out the code and view this page's source to see what's going on. Click a P to make things happen. If you have Firebug, then the logging messages are going to the console. If not, then sorry about all the alerts :)

A "Manager" is a generic handler for multiple objects of a given class. The idea is that you may have a certain widget that appears multiple times on the page. Each instance of this widget gets its own "Daemon" to handle the behavior. Then, there is a "Manager" singleton that provides an interface to the daemons.

With the Manager class, you define the Daemons, and then tell the manager to manage them. On this page, there's a "clickP" class that is managed by "YAHOO.ClickP". Whenever you click a P, it fires the "p_clicked" custom event. By listening to the manager's p_clicked event, we can have one handler that gets called any time any P is clicked on, or we can attach an event handler to a particular daemon's events, etc.

The daemon class should implement a "getId()" method that returns a unique ID for the daemon. If it is not implemented, a default getId method will be added.

If memory leaks are a hazard, then the daemon class should implement an "unload()" method that breaks all circular DOM references. If implemented, this unload function will be called on window.unload.

Each daemon created by the manager will have a manager property added that refers to the manager.

Manager Class members

daemons

An associative array containing the daemons. {daemon.getId() : daemon}.

daemonEvents

A hash table of CustomEvents that can be listened to.

getDaemonById

Send it an ID. Returns a daemon or null.

spawnDaemon

Function that creates a new daemon with the arguments sent to spawnDaemon(). So, (new Manager(Array)).spawnDaemon(5,2,'asdf') would be the same as new Array(5,2,'asdf');

init

Spawns a new daemon and manages it. The arguments passed to this function get passed to the daemon's constructor.

initAll:function(findMethod,findArgs,findScope)

Calls findMethod in findScope with findArgs. findMethod should return an array of "things". Then it calls init() with each "thing" as the argument. This is particularly useful if your daemon class takes an html node as its argument. For example:

myManager.initAll( YAHOO.util.Dom.getElementsByClassName, ['myClass','div'] );
/* Identical to:
var arr = YAHOO.util.Dom.getElementsByClassName('myClass','div);
for( var i = arr.length; i > -1; i--) {
  myManager.init(arr[i]);
}
*/

Manager Custom Events

onCreate

Fired whenever a daemon is created, in the scope of the manager.

daemonEvents

This is a collection of all the custom events that are implemented by the daemon classes. Whenever a daemon has a custom event that fires, the Manager's daemonEvents[eventName] will fire as well. So, if your daemonClass has a onClick custom event, then the manager will also have an onClick custom event. The manager's daemonEvents event will fire in the scope of the daemon that triggered the event, and the type will be the same.

This means that, if your daemons have an onClick event, then subscribing to the manager's onClick handler will be like subscribing to ALL the daemons' onClick events in one fell swoop.

onDaemonEvent

Like the daemonEvents pass-throughs, this event will fire whenever ANY daemon's CustomEvent fires. It will fire in the same scope as the triggering event, and will have the same type string.

Subscribing to the manager's onDaemonEvent will be like subscribing to ALL of the daemons' CustomEvents of every sort.