diff --git a/src/object.js b/src/object.js index e958cd8657..46a16f94f3 100644 --- a/src/object.js +++ b/src/object.js @@ -88,6 +88,27 @@ var object = function () { return m_this; }; + /** + * Report if an event handler is bound to this object. + * + * @param {string|string[]} event An event or list of events to check. + * @param {function} [handler] A function that might be bound. If + * `undefined`, this will report `true` if there is any handler for the + * specified event. + * @returns {boolean} true if any of the specified events are bound to the + * specified handler. + */ + this.geoIsOn = function (event, handler) { + if (Array.isArray(event)) { + return event.some(function (e) { + return m_this.geoIsOn(e, handler); + }); + } + return (m_eventHandlers[event] || []).some(function (h) { + return h === handler || handler === undefined; + }); + }; + /** * Trigger an event (or events) on this object and call all handlers. * diff --git a/tests/cases/object.js b/tests/cases/object.js index 5fab1980a6..965699525a 100644 --- a/tests/cases/object.js +++ b/tests/cases/object.js @@ -20,7 +20,12 @@ describe('geo.object', function () { evtData = {}, foo = new CallCounter(evtData); + expect(obj.geoIsOn('testevent')).toBe(false); obj.geoOn('testevent', foo.call); + expect(obj.geoIsOn('testevent')).toBe(true); + expect(obj.geoIsOn(['testevent', 'anotherevent'])).toBe(true); + expect(obj.geoIsOn('testevent', 'not foo.call')).toBe(false); + expect(obj.geoIsOn('testevent', foo.call)).toBe(true); obj.geoTrigger('anotherevent', evtData); expect(foo.ncalls).toBe(0);