Hi,

Rather than thinking of an Ajax request as a function call, think of
it as a message you send out.  Send it, then get on with other things,
or just wait (where waiting is not a busy-wait where the user can't do
anything).  When you get a reply, deal with the reply.  In this case,
replies are (basically) "complete" and "failure".

So for instance, rather than this:

function getRecord(id)
{
    var record;
    var done;

    // Get the record
    done = false;
    new Ajax.Request("getrecord.php", {
        parameters:     {id: id},
        onSuccess:      function(transport) {
            var json = transport.responseJSON;
            if (json && json.success) {
                record = json.record;
            }
            done = true;
        },
        onFailure:      function(transport) {
            done = true;
        }
    });

    // Very bad idea
    while (!done) {
        // Don't know what you'd do here
    }

    // Got the record?
    if (!record) {
        throw "Unable to retrieve record " + id;
    }

    // Return the record
    return record;
}

...and then calling it like this:

try
{
    record = getRecord(id);
    displayRecord(record);
}
catch (e)
{
    displayError(e.message ? e.message : e);
}

Do this:

function getRecord(id)
{
    new Ajax.Request("getrecord.php", {
        parameters:     {id: id},
        onSuccess:      function(transport) {
            var json = transport.responseJSON;
            if (json && json.success) {
                displayRecord(record);
            }
            else if (json && json.errorMessage) {
                displayError(json.errorMessage);
            }
            else {
                displayError("Unknown error getting record " + id);
            }
        },
        onFailure:      function(transport) {
            displayError("Error communicating with server");
        }
    });
}

Now, that combines the two -- retrieval and display -- which may not
always be what you want.  When you really want them separated, just
use callbacks:

function getRecord(id, success, failure)
{
    new Ajax.Request("getrecord.php", {
        parameters:     {id: id},
        onSuccess:      function(transport) {
            var json = transport.responseJSON;
            if (json && json.success) {
                success(record);
            }
            else if (json && json.errorMessage) {
                failure(json.errorMessage);
            }
            else {
                failure("Unknown error getting record " + id);
            }
        },
        onFailure:      function(transport) {
            failure("Error communicating with server");
        }
    });
}

...and call it like this:

getRecord(id, displayRecord, displayError);

Note that both of those mean that when you call getRecord, it will
return before the record has been retrieved and displayed.

Initially, you'll feel a bit like your thinking is inside-out, but
once you get your head around it, a lot of things are easier if you
always think in terms of your application being in a given state and
waiting for an event to respond to, which takes it to another state.
Some states are "disable all the controls until something finishes",
but most of them aren't, and so it makes for a better user experience.

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

On Feb 27, 5:16 am, BearState <wixelb...@yahoo.com> wrote:
> Oh,
>
> One additional comment ...
>
> If I get clever and try to wait in a while loop for AjaxRequest to set
> a flag that it has completed, the browser complains that some script
> is attempting to make the browser to run slow, do you want to let it
> run?   IE7.
>
> So no, I can't create a slick wait loop like that either.
>
> BearState
>
> On Feb 26, 9:13 pm, BearState <wixelb...@yahoo.com> wrote:
>
> > Hi,
>
> > OK,  so Mr. Noob has made a lot of progress, but has found that when
> > he makes an Ajax.Request(), the response is not set into the
> > javascript variables until after a certain amount of time, which
> > though small, is significant.
>
> > Some knuckleheads might well be provoked to say, "Hey, I didn't get my
> > data.  There's a bug."
>
> > There is no bug, but there is a pain in the ...
>
> > Javascript does not have a sleep function and  using  setTimeOut in
> > this situation is folly because you can not proceed with the code in
> > any semblance until  AjaxRequest full completes returning the data.
> > setTimeOut will only cause one code element to deley.
>
> > How're the Prototype/Scriptaculous gurus handling this?
>
> > BearState aka. Mr. Noob  ( for the moment ).
>
>
--~--~---------~--~----~------------~-------~--~----~
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 prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to