Skip to content

Instantly share code, notes, and snippets.

@devdave
Created March 7, 2013 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devdave/5112936 to your computer and use it in GitHub Desktop.
Save devdave/5112936 to your computer and use it in GitHub Desktop.
Experimental utility for working in Qt WebKit
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
function CapStr(str) {
str[0] = str[0].toUpperCase();
return str;
}
function BuildHandlers(refs, container){
container = container || {};
var selector, element, handlers;
for( selector in refs) {
if( refs.hasOwnProperty(selector) == false ) {
continue;
}
try {
element = jQuery(selector);
handlers = refs[selector];
for(handler in handlers) {
if( handlers.hasOwnProperty(handler) == false){
continue
}
else if(handler == "_name") {
container["get" + CapStr(handler)] = element;
continue;
}
try{
element[handler](handlers[handler].bind(container));
}catch(error){
console.error(error, selector, selector, element, handlers);
}
}
}catch(error){
console.error(error, handler, handlers, container);
}
}
return container;
}
function ControlPanelObj(bridge, jQuery) {
var logAndStop = function(e){
e.preventDefault();
console.dir(arguments);
return false;
}
var panel = {
'#startBtn' : {
click: logAndStop
, _name: "start"
}
, "#stopBtn" : {
click: logAndStop
, _name: "stop"
}
, "#connForm form" : {
submit: logAndStop
, _name: "connForm"
}
}
return BuildHandlers(panel);
}
function ApplicationIsReady(){
window.panel = ControlPanelObj(my_hub, jQuery);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment