At my last job, I'd started using this: http://dpaste.com/176828/
And then writing classes like this:
And then writing classes like this:
Code:
/** Class: Events
* A class to provide a basic event structure.
*/
var Events = Class.extend({
init: function ()
{
this._super();
this._events = {};
},
/** Function: addEvent
* Adds an event handler.
*
* Parameters
* (String) ev - Which event to add the handler to
* (Function) func - The function to add to the event
*/
addEvent: function (ev, func)
{
if (!this._events[ev])
{
this._events[ev] = [];
}
this._events[ev].push(func);
},
/** Function: fireEvent
* Fires an event.
*
* Parameters
* (String) ev - Which event to fire
* (Array) args - Arguments to pass to each event handler
*/
fireEvent: function (ev, args)
{
if (!this._events[ev]) { return; }
for (var i = this._events[ev].length - 1; i >= 0; i--)
{
this._events[ev][i].apply(this, args || []);
}
}
});