Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Synchronous "ajax" with Prototype library

Reply
Thread Tools

Synchronous "ajax" with Prototype library

 
 
keyofdminor@hotmail.com
Guest
Posts: n/a
 
      07-25-2006
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


 
Reply With Quote
 
 
 
 
keyofdminor@hotmail.com
Guest
Posts: n/a
 
      07-25-2006
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.

 
Reply With Quote
 
 
 
 
Jeremy
Guest
Posts: n/a
 
      07-25-2006
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
 
Reply With Quote
 
keyofdminor@hotmail.com
Guest
Posts: n/a
 
      07-25-2006
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();
}
);

 
Reply With Quote
 
Alexis Nikichine
Guest
Posts: n/a
 
      07-26-2006
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();
> }
> );
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between synchronous (proactor pattern) and synchronous model in web server Rickert C++ 0 10-06-2011 04:54 AM
Prototype WTP 0.2 released,this release for Prototype 1.6.0 javascript fish Javascript 0 10-11-2008 07:35 AM
Class prototype vs C function prototype June Lee C++ 2 04-13-2008 08:17 PM
Prototype Object.extend(new Base() | Hash | Hash.prototype) usage: jacobstr@gmail.com Javascript 3 03-27-2007 07:56 AM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57