~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/util.js

  • Committer: mattgiuca
  • Date: 2008-02-25 00:02:33 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:559
Major JavaScript refactor: util.ajax_call is now asynchronous, not
    synchronous.
    This required a change to the interface - now takes a callback and calls
    that, instead of waiting to return.
console.js, browser.js, tutorial.js, tos.js (all users of ajax_call):
    Reworked how they call - all calls now create a callback
    continuationy-thing, and pass that to ajax_call.

Show diffs side-by-side

added added

removed removed

Lines of Context:
551
551
    }
552
552
}
553
553
 
554
 
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
555
 
 * response, and returns an XMLHttpRequest object containing the completed
556
 
 * response.
 
554
/** Makes an asynchronous XMLHttpRequest call to the server.
 
555
 * Sends the XMLHttpRequest object containing the completed response to a
 
556
 * specified callback function.
557
557
 *
 
558
 * \param callback A callback function. Will be called when the response is
 
559
 *      complete. Passed 1 parameter, an XMLHttpRequest object containing the
 
560
 *      completed response.
558
561
 * \param app IVLE app to call (such as "fileservice").
559
562
 * \param path URL path to make the request to, within the application.
560
563
 * \param args Argument object, as described in parse_url and friends.
562
565
 * \param content_type String, optional. Only applies if method is "POST".
563
566
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
564
567
 *      Defaults to "application/x-www-form-urlencoded".
565
 
 * \return An XMLHttpRequest object containing the completed response.
566
568
 */
567
 
function ajax_call(app, path, args, method, content_type)
 
569
function ajax_call(callback, app, path, args, method, content_type)
568
570
{
569
571
    if (content_type != "multipart/form-data")
570
572
        content_type = "application/x-www-form-urlencoded";
575
577
     * used within this function) */
576
578
    var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
577
579
    var xhr = new_xmlhttprequest();
 
580
    xhr.onreadystatechange = function()
 
581
        {
 
582
            if (xhr.readyState == 4)
 
583
            {
 
584
                callback(xhr);
 
585
            }
 
586
        }
578
587
    if (method == "GET")
579
588
    {
580
589
        /* GET sends the args in the URL */
581
590
        url = build_url({"path": path, "args": args});
582
 
        /* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
583
 
         * (No need for a callback function) */
584
 
        xhr.open(method, url, false);
 
591
        /* open's 3rd argument = true -> asynchronous */
 
592
        xhr.open(method, url, true);
585
593
        xhr.send(null);
586
594
    }
587
595
    else
588
596
    {
589
597
        /* POST sends the args in application/x-www-form-urlencoded */
590
598
        url = encodeURI(path);
591
 
        xhr.open(method, url, false);
 
599
        xhr.open(method, url, true);
592
600
        var message;
593
601
        if (content_type == "multipart/form-data")
594
602
        {
604
612
        xhr.setRequestHeader("Content-Length", message.length);
605
613
        xhr.send(message);
606
614
    }
607
 
    return xhr;
608
615
}
609
616