code_berzerker
29 May 2007, 2:52 AM
I'm using Ext with Xajax. I encountered a problem with callbacks there, because there seems to be no way to add multiple callbacks to an event.
I've written the following code as a workaround:
function MultiCallbackManager()
{
this.callbacks = new Array();
}
MultiCallbackManager.prototype.execAll = function()
{
for(var ev in this.callbacks)
for(var i in this.callbacks[ev])
this.callbacks[ev][i]();
}
MultiCallbackManager.prototype.getExecInterfaceFor = function(ev)
{
var callbacks = this.callbacks;
return function()
{
var remove = new Array();
if( !callbacks[ev])
return;
for(var i in callbacks[ev])
if(result = callbacks[ev][i]())
if('object' == typeof result && result.remove)
remove.push(i);
while(ridx = remove.pop())
callbacks[ev].splice(ridx, 1);
}
}
MultiCallbackManager.prototype.add = function(ev, fn)
{
if( !this.callbacks[ev])
this.callbacks[ev] = new Array();
this.callbacks[ev].push(fn);
}
Lets assume I do the following:
cbMngr = new MultiCallbackManager();
xajax.callback.global.onComplete = cbMngr.getExecInterfaceFor('onComplete');
cbMngr.add('onComplete', somefunc);
Now, I dont fully understand how interfaces work, so will it or will not exec somefunc when processing onComplete callback?
I'll greatly appreciate answer!
I've written the following code as a workaround:
function MultiCallbackManager()
{
this.callbacks = new Array();
}
MultiCallbackManager.prototype.execAll = function()
{
for(var ev in this.callbacks)
for(var i in this.callbacks[ev])
this.callbacks[ev][i]();
}
MultiCallbackManager.prototype.getExecInterfaceFor = function(ev)
{
var callbacks = this.callbacks;
return function()
{
var remove = new Array();
if( !callbacks[ev])
return;
for(var i in callbacks[ev])
if(result = callbacks[ev][i]())
if('object' == typeof result && result.remove)
remove.push(i);
while(ridx = remove.pop())
callbacks[ev].splice(ridx, 1);
}
}
MultiCallbackManager.prototype.add = function(ev, fn)
{
if( !this.callbacks[ev])
this.callbacks[ev] = new Array();
this.callbacks[ev].push(fn);
}
Lets assume I do the following:
cbMngr = new MultiCallbackManager();
xajax.callback.global.onComplete = cbMngr.getExecInterfaceFor('onComplete');
cbMngr.add('onComplete', somefunc);
Now, I dont fully understand how interfaces work, so will it or will not exec somefunc when processing onComplete callback?
I'll greatly appreciate answer!