Skip to content

Instantly share code, notes, and snippets.

@shaneriley
Created September 29, 2011 14:59
Show Gist options
  • Save shaneriley/1250910 to your computer and use it in GitHub Desktop.
Save shaneriley/1250910 to your computer and use it in GitHub Desktop.
$.fn.hasEvent
$.fn.hasEvent = function(event, fn) {
if (!event) { return this; }
var has = {
event: false,
namespace: false,
handler: false
},
events = this.data("events"),
namespace = event.split(".");
event = namespace.shift();
namespace = namespace.join(".");
if (events) {
if (!namespace) { has.namespace = true; }
if (!fn) { has.handler = true; }
if (event in events) {
$.each(events[event], function(i, v) {
if (namespace) {
if (namespace === v.namespace && event === v.type) {
has.namespace = true;
has.event = true;
if (fn && fn === v.handler) { has.handler = true; }
}
}
else {
if (event === v.type) {
has.event = true;
if (fn && fn === v.handler) { has.handler = true; }
}
}
});
}
}
return has.event && has.namespace && has.handler;
};
Examples: http://jsbin.com/ehezux/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment