Hi,

My guess is the problem lies in your code that calls exFetch (or at
least code elsewhere), since that quoted code not only doesn't call
the handler right away, it won't call it _at all_.  This anonymous
function:

function (transport) {
    window[callBack]
}

...doesn't call the handler.  It looks up a property name on the
window object (and presumably gets back a function reference), and
then throws away the result.  To call the function, you'd have to put
() after the expression that gives you the function reference (or use
call(), apply(), etc.).  So this _defines_ a function:

    x = function() { ... };

...and this _refers_ to the function without calling it (no parens or
the like), assigning the function reference to 'y':

    y = x;

This _calls_ it, assigning the function's *result* to 'z':

    z = x();

> Since the callback could be any function, I'm
> passing the callback function as a string and calling it via window
> [callBack]...

If that's your only reason for passing around the function name as a
string (that it could be any function), you don't gain anything by
doing that.  Functions are first class objects, so you can pass them
by reference -- you do it all the time, if you use any of Prototype's
Ajax wrappers, or Enumerable#each, or any of several other bits of
Prototype.  In fact, you did it in the quoted code, with the anonymous
function.

Here's a function that accepts a callback and uses it as the
onComplete handler of an Ajax.Updater call:

    function wrapAjaxUpdater(url, element, callback) {

        new Ajax.Updater(url, element, {
            onComplete:  callback
        });
    }

(It also has the advantage of not creating a further closure.)

An example of calling it:

    wrapAjaxUpdater('showsomething.php', 'somediv', function
(transport) {
        alert('It finished!');
    });

Or call it with a named handler (note the absense of parens after the
handler's name):

    wrapAjaxUpdater('showsomething.php', 'somediv', handleCompletion);

...where handleCompletion is defined somewhere and can be reused:

    function handleCompletion(transport) {
        alert('It finished!');
    }

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Apr 14, 10:44 pm, Diodeus <[email protected]> wrote:
> Here is my function. It is a general purpose wrapper for various AJAX
> call in my program. Since the callback could be any function, I'm
> passing the callback function as a string and calling it via window
> [callBack], which kinda of works except it's not waiting for the
> actual AJAX call to complete - it fires right away. Since I wrapped
> the callback in an anonymous function, I would have expected the
> execution to be deferred, but it isn't.
>
> Where have I gone wrong?
>
> function exFetch(url,element,callBack) {
>         now = new Date() //break cache
>         //$('lastAjax').innerHTML = url +"|"+element
>         new Ajax.Updater(element, url+'&_UserReference='+key+'&now='+now,
> {method:'get',evalScripts:true,onComplete:function(transport) {
>                 window[callBack]
>           }
>         });
>
> }
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to