Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an object.geoIsOn function to determine if an event is bound. #768

Merged
merged 2 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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