Skip to content

Commit

Permalink
adding generic event handling inside the object class
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeezley committed Mar 14, 2014
1 parent 8ecc17a commit 7325742
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion src/core/object.js
Expand Up @@ -4,7 +4,7 @@
*/

/*jslint devel: true, forin: true, newcap: true, plusplus: true*/
/*jslint white: true, indent: 2*/
/*jslint white: true, indent: 2, unparam: true*/

/*global geo, ogs, inherit, $, HTMLCanvasElement, Image*/
/*global vgl, document*/
Expand All @@ -23,6 +23,81 @@ geo.object = function(cfg) {
if (!(this instanceof geo.object)) {
return new geo.object();
}

var m_eventHandlers = {};

//////////////////////////////////////////////////////////////////////////////
/**
* Bind an event handler to this object
*/
//////////////////////////////////////////////////////////////////////////////
this.on = function (event, handler) {
if (Array.isArray(event)) {
event.forEach(function (e) {
this.on(e, handler);
});
return this;
}
if (!m_eventHandlers.hasOwnProperty(event)) {
m_eventHandlers[event] = [];
}
m_eventHandlers[event].push(handler);
return this;
};

//////////////////////////////////////////////////////////////////////////////
/**
* Trigger an event (or events) on this object and call all handlers
*/
//////////////////////////////////////////////////////////////////////////////
this.trigger = function (event, args) {
if (Array.isArray(event)) {
event.forEach(function (e) {
this.trigger(e, args);
});
return this;
}
if (m_eventHandlers.hasOwnProperty(event)) {
m_eventHandlers[event].forEach(function (handler) {
handler(args);
});
}
return this;
};

//////////////////////////////////////////////////////////////////////////////
/**
* Remove handlers from an event (or an array of events).
*
* @param arg a function or array of functions to remove from the events
* or if falsey remove all handlers from the events
*/
//////////////////////////////////////////////////////////////////////////////
this.off = function (event, arg) {
if (Array.isArray(event)) {
event.forEach(function (e) {
this.off(e, arg);
});
return this;
}
if (!arg) {
m_eventHandlers[event] = [];
} else if (Array.isArray(arg)) {
arg.forEach(function (handler) {
this.off(event, handler);
});
return this;
}
// What do we do if the handler is not already bound?
// ignoring for now...
if (m_eventHandlers.hasOwnProperty(event)) {
m_eventHandlers[event] = m_eventHandlers[event].filter(function (f) {
return f !== arg;
});
}
return this;
};

vgl.object.call(this);

return this;
Expand Down

0 comments on commit 7325742

Please sign in to comment.