PDA

View Full Version : general javascript question (MultiCallbackManager class)



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!

Animal
29 May 2007, 3:09 AM
You're asking questions about "xajax" here?

Well I don't know anything about it, but if you used Ext's server communication classes, you can add as many handlers as you like to events.

Events and callbacks are different things.

code_berzerker
29 May 2007, 3:12 AM
Ok it seems I've been able to deal with it on my own:



// test code
var cbmTest = cbMngr.getExecInterfaceFor('test');
cbMngr.add('test', function(){alert('success')});
cbmTest();


The above works fine - alerts 'success'.

So now I can declare callback manager for each type of event I want and add/remove any number of functions at any time.

I rock! >:)

code_berzerker
29 May 2007, 3:37 AM
You're asking questions about "xajax" here?

xajax has nothing to do with my question really, I just mentioned it to precede questions why I need such a thing if Ext has it already.



Well I don't know anything about it, but if you used Ext's server communication classes, you can add as many handlers as you like to events.

Well I have some technical reasons to use Ext + xajax, but its kindof OT.



Events and callbacks are different things.
I believe I havent given any reason to make you think other way ;)
I assure you I can differ events, callbacks and handlers each from another.

Animal
29 May 2007, 3:52 AM
Fairy nuff, I'll ask google about it and see if it has any extra stuff that Ext can't do.

If it does, then possibly Ext should offer those extras...

code_berzerker
29 May 2007, 4:08 AM
The main reason I use xajax along with Ext is that it (xajax) is (simplyfying) kind of abstraction layer for php to use ajax easier. It provides both server and client side APIs. And as I'm not (or at least I wasnt till I found Ext) kin on using javascript and also I'm totally new to Ajax I prefered to have some wrapper to dop the dirty job for me. Other than that xajax has very basic feature set.

Hope that helps.

cheers :)

code_berzerker
29 May 2007, 6:13 AM
I have updated code in original post. Now you can try this code:



function cbmTest()
{
var test = cbMngr.getExecInterfaceFor('test');
cbMngr.add('test', function(){alert('success');});
cbMngr.add('test', function(){alert('success2');return {remove:true}});
// below simulating event 'test' by just explicit call to its handler.
test();
test();
}
cbmTest();


New feature is that if callback returns object {remove:true} it gets removed after first execution. This is in case of need executing function only once (eg. removal of preloader-masking).