Synchronous "ajax" with Prototype library

Discussion in 'Javascript' started by keyofdminor@hotmail.com, Jul 25, 2006.

  1. Guest

    Folks,

    Short version:

    Has anyone tried a synchronous call ("SJAX") to a server with the
    Prototype library? I'm curious if there is a bug in the library
    (possible) or if I am making mistake (probable).

    Longer version:

    I am updating a tree structure by using an Ajax call with Prototype.
    The response handler adds nodes to the DOM (w3c-style -- not
    innerHTML). However, other Javascript does not "see" the new nodes.
    The logging suggests that the handler is being fired off in the
    background asynchronously.

    Here's a snippet:

    var myAjax = new Ajax.Request( myUrl ,
    {
    method: 'get',
    parameters: '',
    options: { asynchronous: false }, // ??
    onComplete: function(response) {
    var text = response.responseText;
    treeData = eval('(' + text + ')');
    myPopulate(treeData);
    }
    });

    checkForNodes(); // this should not execute until after the myPopulate
    handler completes

    I'm a Java guy who does not work with Javascript often. Is there an
    inherent problem with this setup?

    Any help is greatly appreciated... crunch time here.

    thanks,
    Mike
    , Jul 25, 2006
    #1
    1. Advertising

  2. Guest

    ps. Note that my concern here is timing... The nodes are indeed created
    so the data from the server is fine etc. It's just that the
    checkForNodes() code asks for the children of a div and it doesn't work
    until the 2nd or 3rd click.
    , Jul 25, 2006
    #2
    1. Advertising

  3. Jeremy Guest

    wrote:
    > Folks,
    >
    > Short version:
    >
    > Has anyone tried a synchronous call ("SJAX") to a server with the
    > Prototype library? I'm curious if there is a bug in the library
    > (possible) or if I am making mistake (probable).
    >
    > Longer version:
    >
    > I am updating a tree structure by using an Ajax call with Prototype.
    > The response handler adds nodes to the DOM (w3c-style -- not
    > innerHTML). However, other Javascript does not "see" the new nodes.
    > The logging suggests that the handler is being fired off in the
    > background asynchronously.
    >
    > Here's a snippet:
    >
    > var myAjax = new Ajax.Request( myUrl ,
    > {
    > method: 'get',
    > parameters: '',
    > options: { asynchronous: false }, // ??
    > onComplete: function(response) {
    > var text = response.responseText;
    > treeData = eval('(' + text + ')');
    > myPopulate(treeData);
    > }
    > });
    >
    > checkForNodes(); // this should not execute until after the myPopulate
    > handler completes
    >
    > I'm a Java guy who does not work with Javascript often. Is there an
    > inherent problem with this setup?
    >
    > Any help is greatly appreciated... crunch time here.
    >
    > thanks,
    > Mike
    >
    >


    Synchronous server calls are generally considered a Bad Idea. I know it
    seems easier to not have to mess with the callback, but the problem is
    that while the call is blocking, the browser typically locks up. This
    could be seen as an implementation problem with the browsers, but AFAIK,
    every browser that implements javascript HTTP Requests behaves this way.

    The consequence of this is that if your server is slow or not
    responding, the browser can lock up for a long time or even permanently.

    Try porting your code to the callback model - it's actually pretty easy,
    although it might seem like a pain at first.

    Jeremy
    Jeremy, Jul 25, 2006
    #3
  4. Guest

    thanks, Jeremy... Your post helped me realize, with stunning insight,
    that with some refactoring it doesn't matter if the call is
    asynchronous.

    For any one else reading this, the gist of the idea is if

    function checkNodes() {
    doStuff1();
    doStuff2();
    }

    then by breaking out doStuff2() into its own function I can do:

    var myAjax = new Ajax.Request( url, function() {
    // as before
    doStuff2();
    }
    );
    , Jul 25, 2006
    #4
  5. wrote:

    > thanks, Jeremy... Your post helped me realize, with stunning insight,
    > that with some refactoring it doesn't matter if the call is
    > asynchronous.


    Yes, that's a great thing to realize. However, in order to automate the
    refactoring you describe, did anyone have some experience with Narrative
    Javascript (http://neilmix.com/narrativejs/doc/index.html ) ? If I
    understand correctly, it is a javascript preprocessor that should allow
    you to write your checkNodes function as follows:

    function checkNodes() {
    doStuff1->(); // This operation will block, so please
    // resume execution only after its complete
    doStuff2();
    }


    Cheers,

    Alexis

    > For any one else reading this, the gist of the idea is if
    >
    > function checkNodes() {
    > doStuff1();
    > doStuff2();
    > }
    >
    > then by breaking out doStuff2() into its own function I can do:
    >
    > var myAjax = new Ajax.Request( url, function() {
    > // as before
    > doStuff2();
    > }
    > );
    >
    Alexis Nikichine, Jul 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. June Lee
    Replies:
    2
    Views:
    790
    Jim Cobban
    Apr 13, 2008
  2. Rickert
    Replies:
    0
    Views:
    644
    Rickert
    Oct 6, 2011
  3. Jake Barnes
    Replies:
    1
    Views:
    75
    Thomas 'PointedEars' Lahn
    Feb 7, 2006
  4. Replies:
    9
    Views:
    176
    Thomas 'PointedEars' Lahn
    May 26, 2006
  5. Replies:
    3
    Views:
    259
Loading...

Share This Page