|
|
This page documents the core of the Custom Events code. Docs for the rest of the code (the tracer and broker) will be added as time permits.
Mix Event.Publisher into your class or object if it needs to publish custom events for Event.Listeners. Allows your objects to use the same event features as DOM elements, because it uses hidden DOM elements to implement the custom events.
Found in event_mixins.js (and the version without debugging, event_mixins_notrace.js).
Mix Event.Listener into your class if it needs to act on the event notifications sent by any publishers.
Found in event_mixins.js (and the version without debugging, event_mixins_notrace.js).
Private. If a call to listenForEvent or stopListeningForEvent omits the name of the listener function, getEventHandlerName is used to find the "correct" name of the listener function.
The "correct" name of the handler function is determined by upper-casing the first character of the event name, replacing all /[ _]/ with -, camelizing the result, and then prepending "on". So if the event's name is "i just did_something", this function would return "onIJustDidSomething", and the listener code would then assume that was the name of the listener function.
Adds the methods create and dispatch to Prototype's Event object.
Found in event_mixins.js (and the version without debugging, event_mixins_notrace.js).
Browser-agnostic method for creating new click events.
Could/should be generalized for creating events of any type, but this will be difficult because the browsers all do this so very differently (createEvent versus createEventObject, and HTMLEvents versus MouseEvents...). For now, this method is only usable for click events.
event_data is any data the Publisher wants attached to the event for use by the Listeners, can_bubble and cancelable are straight out of the DOM event api, fl_dispatch is a boolean which (if true) causes the event to be dispatched to target as soon as it is created, and target is the DOM element which is to receive the event (as in, "pretend I clicked on [target]"). target is only used if fl_dispatch is true.
The broker allows your publishers and listeners (subscribers) to remain unaware of each other. Publishers register their events (by name) with the broker. Listeners subscribe only to the broker, rather than to the publishers (the broker takes care of subscribing the listeners to the publishers, automatically).
Use of Event.Broker changes the events model so that listeners just need to know what events they care to receive, and don't need to know anything about which objects on the page will actually produce those events.
Event.Broker is especially useful in three situations:
In all of those cases, the benefit of the broker is that the listeners only need to know about the one, central Event.Broker object.
Public. Called by a Publisher, usually when it is first created/initialized.
event_types is an array of strings, where each string is the name (type) of one event produced by this publisher. publisher is the Publisher object which is being registered (usually 'this').
window.eventBroker.registerEventsPublisher( ['new msg arrived', 'msg deleted'], this )
Any Listeners which have already registered with the Broker for any of the events in event_types will be automatically subscribed to this publisher, immediately. New Listeners (for events of the types produced by this Publisher) will be subscribed to this publisher as soon as they register with the Broker.
Public. Called by a Publisher, usually when the Publisher object is about to be destroyed.
This is the opposite of .registerEventsPublisher().
event_types is an array of strings, where each string is the name (type) of one event produced by this publisher. publisher is the Publisher object which is being un-registered with the Broker (usually 'this').
All of the Listeners which have subscribed to this Publisher's events will be automatically unsubscribed by the Broker, and then the Broker will remove the Publisher from its internal lists of Publishers so that it will no longer receive any new subscribers.
Public. Called by a Listener, usually when it is first created/initialized. However, you do not call this method directly. It's called by the Event.Listener interface. (See example, below.)
event_type is the name/type of the event that the Listener wants to receive. event_listener is the Listener object itself (usually 'this'). useCapture is the DOM-standard parameter to indicate at which phase the event should be sent to the Listener.
After registering with the Broker, the Broker will make sure that the Listener is subscribed to all Publishers which produce the events for which the Listener has subscribed. (This includes Publishers which which register before or after the Listener registered.)
this.listenForEvent( window.eventBroker, 'new msg arrived', false );
this.listenForEvent( window.eventBroker, 'msg deleted', false, 'onMsgDeleted' );
Public. Called by a Listener when it is no longer interested in receiving a given type of event (such as when it is being destroyed!) You do not call this method directly. It's called by the Event.Listener interface. (See example, below.)
event_type is the name/type of event that the Listener is no longer interested in receiving. event_listener is a reference to the Listener instance itself (usually 'this'). useCapture is the DOM-standard parameter that indicated at which phase the event was to be sent to the Listener: the boolean value of this param must match the original call to addEventListener.
Unregistering with the broker will cause the Listener to be unsubscribed from all Publishers of events of type event_type.
this.stopListeningForEvent( window.eventBroker, 'new msg arrived', false );
this.stopListeningForEvent( window.eventBroker, 'msg deleted', false, 'onMsgDeleted' );
Calls to an event publisher's .dispatchEvent() method may include an optional data param, which can be of any type. This data (or null if not provided) can be accessed by the listeners via event.event_data.data.
The event_data object that is attached to every custom event includes three properties:
Page last updated: 9/6/2006
Until August 31
My Amazon sales
benefit the PMC
|
TruerWords
is Seth Dillingham's personal web site. More than the sum of my parts. |