Skip to content

Instantly share code, notes, and snippets.

@19h
Created April 16, 2012 01:27
Show Gist options
  • Save 19h/2395868 to your computer and use it in GitHub Desktop.
Save 19h/2395868 to your computer and use it in GitHub Desktop.
Automated Eventfiring HTML5 Javascript fireEvent Titanium NODE.JS-Compatbile
fire = function (b, d, e) {
if ("undefined" !== typeof module && module.exports) {
if (b instanceof require("events").EventEmitter) {
// Note for me (Array.prototype.reverse.apply(arguments)).pop() // remove first argument, give rest, arguments instanceof Object!
// NOTE: As Node.js's emit takes a name and an array, q should be typeof Array.. so we're fixing it here.
for (var c = 0, f = []; c < e.length; ++c) void 0 !== f[c] && (e[c] = f[c]);
return b.emit(d, f)
}
return !1
}
document.createEvent ? (a = document.createEvent("HTMLEvents"), a.initEvent(d, !0, !0)) : (a = document.createEventObject(), a.eventType = "on" + d);
a.eventName = d;
a.memo = e || {};
return document.createEvent ? b.dispatchEvent(a) : b.fireEvent(a.eventType, a)
};
//Test: NodeJS
if ("undefined" !== typeof module && module.exports) { // just to be sure, if being executed without thinking, we better do not use the Node.JS Part.
// TODO: FIX!
z = function () {
return this.prototype = new require("events").EventEmitter();
}
console.log(z, z());
z().on("foo", function (x){ // function is called with all arguments but the first given to emit
return console.log(x);
})
fire(z, "foo", {0:"oh yeah"});
} else { // this is a browser instance or some beenie-wheenie nodeJS-fake
//Test: Browser
document.body.addEventListener("test", function (b) {
console.log(b.memo.text)
});
fire(document.body, "test", {
text: "Win!"
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment