fabric-engine/JSSceneGraph — Issue #107: Refactor sceneGraph's resourceLoad to use generic JS event system
Author: jcgagnon
Created: 2011-12-21 21:20:44+00:00
State: open

From Phil's mail:

We have a generic system for setting up events in Fabric.

scene.addEventHandlingFunctions(mynode) ;

and what that does is adds 3 functions to the passed in obj.

addEventListener(type, fn)
removeEventListener(type, fn)
fireEvent(type, evt)

Then, you can add event listeners by calling...

mynode.addEventListener('myevent', function(evt){ do stuff });

from within mynode you can fire events by calling....

mynode.fireEvent('myevent', { data:bla, moredata:bla2 } );

This generic system makes it easy to set up events, and automatically
handles such things as event listener removal. It is better to use
this generic system, than to add custom event registration
functions....

e.g.

resourceLoadNode.pub.addOnLoadSuccessCallback ....

this should be handled by the generic interface.

resourceLoadNode.pub.addEventListener('onload', function(evt){... } );

The benefits of using the generic interface are...
- consistency across all nodes. All even registration should follow
the same pattern everywhere. This simplifies learning Fabric.
- consistency with the browser dom Level 2 events. The generic event
registration interface mirrors the way the new DOM specification is
moving.
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-overview
events such as
window.onresize()
are being replaced with...
window.addEventListener('resize', Foo);
- Less code. Automatic systems such as event listener removal.(which
otherwise may be overlooked)
- better debugging. We can easily log all events in the system if they
go thought a generic interace.
- minimal interfaces. We can have 5 different events fired from a node
without 15 extra functions for handling them.

Events are an ideal way for different systems to interact. Events
represent a one-to-(0-many) communication, which makes them better
than manually calling functions between systems. The decoupling is
important to enable many systems to interact without becoming overly
complex.

P.S. I notice that maybe fireEvent should be renamed to dispatchEvent.
I will review the system soon.