Skip to content

Instantly share code, notes, and snippets.

@danielkza
Created September 14, 2012 22:21
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 danielkza/3725305 to your computer and use it in GitHub Desktop.
Save danielkza/3725305 to your computer and use it in GitHub Desktop.
registerFooCallbacks
// Scans an object for WSH-Panel callbacks, and registers them.
function registerFooCallbacks(obj)
{
if(!(obj instanceof Object))
return false;
// grab a reference to the global context
var global = (function() { return this; })();
for(var attr in obj) {
if(attr.indexOf("on_") != 0)
continue;
// Check if we have not already created our handler for this callback type.
// Check first if there's a version of the callback in the global scope.
// If affirmative, check if it's actually ours, and if it's not, save the
// current function, so it can still be called later.
var original_func = null;
if(typeof global[attr] == 'function') {
if('callback_objs' in global[attr]) {
global[attr].callback_objs.push(obj);
continue;
}
original_func = global[attr];
}
// We need a wrapper taking attr as an argument to capture it by value,
// not reference.
global[attr] = (function(attr) {
var f = function() {
var original_func = arguments.callee.original_func;
if(typeof(original_func) == 'function')
original_func.apply(global, arguments);
var callback_objs = arguments.callee.callback_objs;
if(callback_objs) {
for(var i = 0; i < callback_objs.length; i++) {
var obj = callback_objs[i];
if(!(obj instanceof Object)) {
callback_objs.slice(i, 1);
i--;
continue;
}
if(typeof obj[attr] == 'function')
obj[attr].apply(obj, arguments);
}
}
};
return f;
})(attr);
global[attr].callback_objs = [obj];
global[attr].original_func = original_func;
}
return true;
};
// What follows is example code: if you just want to test you can copy everything into a WSH-Panel instance,
// otherwise skip everything below this line.
function MyPlayMonitor(id)
{
this.id = id;
registerFooCallbacks(this);
};
MyPlayMonitor.prototype.on_play_order_changed = function(new_order_index)
{
fb.trace("Play order changed! (id = " + this.id + ")");
};
MyPlayMonitor.prototype.on_playback_pause = function(state)
{
fb.trace("Playback state changed! (id = " + this.id + ")");
};
function on_playback_pause(state)
{
fb.trace("Global callback, reporting for duty.");
};
var play_orders = [new MyPlayMonitor("foo"), new MyPlayMonitor("bar")];
// Scans an object for WSH-Panel callbacks, and registers them.
function registerFooCallbacks(obj)
{
if(!(obj instanceof Object))
return false;
// grab a reference to the global context
var global = (function() { return this; })();
for(var attr in obj) {
if(attr.indexOf("on_") != 0)
continue;
// Check if we have not already created our handler for this callback type.
// Check first if there's a version of the callback in the global scope.
// If affirmative, check if it's actually ours, and if it's not, save the
// current function, so it can still be called later.
var original_func = null;
if(typeof global[attr] == 'function') {
if('callback_objs' in global[attr]) {
global[attr].callback_objs.push(obj);
continue;
}
original_func = global[attr];
}
// We need a wrapper taking attr as an argument to capture it by value,
// not reference.
global[attr] = (function(attr) {
var f = function() {
var original_func = arguments.callee.original_func;
if(typeof(original_func) == 'function')
original_func.apply(global, arguments);
var callback_objs = arguments.callee.callback_objs;
if(callback_objs) {
for(var i = 0; i < callback_objs.length; i++) {
var obj = callback_objs[i];
if(!(obj instanceof Object)) {
callback_objs.slice(i, 1);
i--;
continue;
}
if(typeof obj[attr] == 'function')
obj[attr].apply(obj, arguments);
}
}
};
return f;
})(attr);
global[attr].callback_objs = [obj];
global[attr].original_func = original_func;
}
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment