473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Synchronous "ajax" with Prototype library

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
ke*********@hotmail.com

Jul 25 '06 #1
4 10523
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 '06 #2
ke*********@hotmail.com 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
ke*********@hotmail.com
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
Jul 25 '06 #3
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 '06 #4
ke*********@hotmail.com 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();
}
);
Jul 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Larry Bud | last post by:
More of a design question than technical.... combining Ajax technology with ASP... Top portion of the page contains several Select boxes which are dependant upon one another. Ajax handles that...
3
by: Tony | last post by:
I've done a fair bit of searching on this, but I want to be certain about what I've found - so here goes with a long example and three questions: For clarity, let me give an example (a number of...
6
by: blueapricot416 | last post by:
I have some javascript in a standard HTML page that uses the ubiquitous "XMLHttpRequest" to send data to a remote ASP page. If that page "answers back" by sending a string using a simple...
1
by: Tony Lawrence | last post by:
Probably a lot of you here are already old hands at Ajax, but I haven't started doing this yet. Part of the reason was that I really didn't understand where Ajax would be useful to my own site,...
3
by: caston | last post by:
Well, everybody can now agree with the fact that the Ajax hype is over. Still multiple Ajax Frameworks are flourishing, aren't they? So, last night I questioned myself with the following: "When...
2
by: pe3no | last post by:
Hi, I'm going to create applications PHP + AJAX + Linux + Apache + Postgres. - Other Open Source technologies / Frameworks, etc. also :) Is this book http://www.amazon.com/gp/reader/0471777781...
1
by: mark4asp | last post by:
Which should I use: 1. "ASP.NET AJAX-Enabled Web Site" or 2. "AJAX Control Toolkit Web Site"? In the first, controls from the control toolkit start as: <cc1:SomeControl></cc1> In the 2nd,...
5
by: Sommer.pde | last post by:
It took me some time to find out when and why this happens. When the window of Internet Explorer is closed with an AJAX call still pending, something funny happens. Do it twice, and you will have...
11
by: Jonathan Wood | last post by:
Can anyone point me to any good resources on adding AJAX to a page once the page has already been created? I know VS2008 has options to add AJAX pages, but I didn't select those options when the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.