Skip to content

Commit

Permalink
Merge pull request #768 from OpenGeoscience/object-geoison
Browse files Browse the repository at this point in the history
Add an object.geoIsOn function to determine if an event is bound.
  • Loading branch information
manthey authored Feb 7, 2018
2 parents a8e8c97 + 30430c4 commit 6f89eb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions tests/cases/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 6f89eb1

Please sign in to comment.