Parallele Ajax, parameters to response handler?

D

Daniel Loose

hi dear folks,

i'm new to ajax. simple request no problem - but it seems when I set
the response handler function, I can only pass the function name and
not give parameters, so I have a problem when starting e.g. 5 ajax
requests parallele. how to do that correctly? for one case, where I
used *different* resp. handlers, I solved the problem by global
variables - but now I have a loop of e.g. 20 calls to the *same*
r.handler and I don*t know how to tell the function *which* request of
the 20 to use. I expect the solution to be simple but how... nothing
found on a quick google, sorry...

thanx in advance! have a nice sunday and week, daniel
 
D

David Mark

hi dear folks,

i'm new to ajax. simple request no problem - but it seems when I set

New to what? I assume you are using some Ajax library.
the response handler function, I can only pass the function name and
not give parameters, so I have a problem when starting e.g. 5 ajax
requests parallele. how to do that correctly? for one case, where I
used *different* resp. handlers, I solved the problem by global
variables - but now I have a loop of e.g. 20 calls to the *same*
r.handler and I don*t know how to tell the function *which* request of
the 20 to use. I expect the solution to be simple but how... nothing

You do it with anonymous functions and closures or you can use the
Function constructor.

Without seeing your original code, I can't tell you how to modify it.
 
D

Daniel Loose

On Sun, 12 Aug 2007 05:05:36 -0700, David Mark

Hi David, thank you for your fast reply.
New to what? I assume you are using some Ajax library.

New to Ajax. No I don't like libraries if not necessary. Actually I
build my own, but that's another topic. I always wish to have/ use/
understand the "original" code and objects... in the beginning.
You do it with anonymous functions and closures or you can use the
Function constructor.

Without seeing your original code, I can't tell you how to modify it.

ok it's just like [the requestId stuff is my workaround for calling
different response handlers in parallele as mentioned]

function wuwinoAjax (script, responseHandler, paraString, reqId) {

requestId = (reqId) ? reqId : 'firstRequest' ;

httpRequest[requestId] = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...

httpRequest[requestId] = new XMLHttpRequest();
if (httpRequest[requestId].overrideMimeType)
httpRequest[requestId].overrideMimeType('text/xml');

} else if (window.ActiveXObject) { // IE

try {
httpRequest[requestId] = new
ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {

try {
httpRequest[requestId] = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!httpRequest[requestId]) { return false; }

httpRequest[requestId].onreadystatechange = responseHandler;
httpRequest[requestId].open('GET', rootURL +
'wuwino/ajax.php?script=' + script + paraString, true);
httpRequest[requestId].send(null);
}

httpRequest = new Array();
requestToUse = new Array();
//default
requestToUse['updateTraittypeBoxes'] = 'firstRequest';

<<

.... called with eg...

requestToUse['updateTraittypeBoxes'] = 'secondRequest';
ajax('traittypes', updateTraittypeBoxes, 'traittypeId=55',
'secondRequest');

<<

.... and the responseHandler does...

function updateTraittypeBoxes () {

http_request =
httpRequest[requestToUse['updateTraittypeBoxes']];

....
}

<<

Thanx again in advance!
 
D

David Mark

On Sun, 12 Aug 2007 05:05:36 -0700, David Mark


Hi David, thank you for your fast reply.
New to what? I assume you are using some Ajax library.

New to Ajax. No I don't like libraries if not necessary. Actually I
build my own, but that's another topic. I always wish to have/ use/
understand the "original" code and objects... in the beginning.

Good.

[snip]


ok it's just like [the requestId stuff is my workaround for calling
different response handlers in parallele as mentioned]



function wuwinoAjax (script, responseHandler, paraString, reqId) {

requestId = (reqId) ? reqId : 'firstRequest' ;

httpRequest[requestId] = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...

IE7 also ends up in here, so the comment isn't accurate.
httpRequest[requestId] = new XMLHttpRequest();
if (httpRequest[requestId].overrideMimeType)
httpRequest[requestId].overrideMimeType('text/xml');

Why are you overriding the MIME type? Is it not set properly on the
server?
} else if (window.ActiveXObject) { // IE

try {
httpRequest[requestId] = new
ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {

try {
httpRequest[requestId] = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!httpRequest[requestId]) { return false; }

httpRequest[requestId].onreadystatechange = responseHandler;
httpRequest[requestId].open('GET', rootURL +
'wuwino/ajax.php?script=' + script + paraString, true);
httpRequest[requestId].send(null);

}

httpRequest = new Array();
requestToUse = new Array();

You are misusing arrays here. In this context, you should Object
objects.
//default
requestToUse['updateTraittypeBoxes'] = 'firstRequest';

<<

... called with eg...



requestToUse['updateTraittypeBoxes'] = 'secondRequest';
ajax('traittypes', updateTraittypeBoxes, 'traittypeId=55',
'secondRequest');

<<

... and the responseHandler does...

Actually this is a ready state handler.
function updateTraittypeBoxes () {

http_request =
httpRequest[requestToUse['updateTraittypeBoxes']];

Since this function is called as a method of the request object, you
can reduce this to:

http_request = this;

It doesn't appear that you need to keep track of anything else, so
just delete the two arrays.
 
R

Richard Cornford

David said:
On Aug 12, 8:25 am, Daniel Loose wrote:
function updateTraittypeBoxes () {

http_request =
httpRequest[requestToUse['updateTraittypeBoxes']];

Since this function is called as a method of the request
object, you
can reduce this to:

http_request = this;
<snip>

At least some XML HTTP request object (ActiveX versions on windows IE)
do not call their - onreadystatechange - handlers as method of the
request object. That was not a good design decision, but it is why you
almost never see example handlers trying to use - this - to reference
the object.

Richard.
 
D

David Mark

David said:
On Aug 12, 8:25 am, Daniel Loose wrote:
function updateTraittypeBoxes () {
http_request =
httpRequest[requestToUse['updateTraittypeBoxes']];
Since this function is called as a method of the request
object, you
can reduce this to:
http_request = this;

<snip>

At least some XML HTTP request object (ActiveX versions on windows IE)
do not call their - onreadystatechange - handlers as method of the
request object. That was not a good design decision, but it is

After I posted, it occurred to me that MS might have a monkey wrench
in the works of that solution. I've never ran into it as my object
for Ajax requests handles the readystatechange events internally and
passes a reference to the request object to external handlers.
 
M

Martin Honnen

Richard said:
At least some XML HTTP request object (ActiveX versions on windows IE)
do not call their - onreadystatechange - handlers as method of the
request object. That was not a good design decision, but it is why you
almost never see example handlers trying to use - this - to reference
the object.

Mozilla in released versions (including Firefox 2.0) makes the |this|
object in the onreadystatechange handler the handler function itself.
 
D

David Mark

Since this function is called as a method of the request object, you
can reduce this to:

http_request = this;

As Richard pointed out, MS screwed up their implementation of Ajax
event handling. What you need to do is define an inner function in
wuwinoAjax and use it to handle readystatechange events. Make your
callback inside that function and pass a reference to the request
object. As the request object (and the callback function) will be
preserved in a closure that creates a circular reference, make sure
you set the onreadystatechange property of the request object to null
when the request finishes loading.
 
B

Ben Amada

David said:
IE7 also ends up in here, so the comment isn't accurate.

In most cases, IE7 would support XMLHttpRequest. But not always since
native XMLHTTP support can easily be turned off in IE7 on the Advanced tab
of the Internet Options dialog window.
 
D

dhtmlkitchen

hi dear folks,

i'm new to ajax. simple request no problem - but it seems when I set
the response handler function, I can only pass the function name and
not give parameters, so I have a problem when starting e.g. 5 ajax
requests parallele. how to do that correctly?

YUI recommends adding expando properties to the callback function
object. This works, but is horrible design.

// Non-explicit and error-prone approach.
myCallback.arg = "poop";

I recommend using a different approach. Not the uid approach -- an
observer-based approach.

The implementation would look something like:
var req = Ajax.createConnection( url, "POST", true, reqId );
EventRegistry.add( req, Ajax.Events.SUCCESS, cbSuccessFunction );
req.send( aForm.getDataSetString() );

function cbSuccessFunction( ajaxEvent ) {
var reqId = ajaxEvent.reqId;
var userPanel = Panel.getById( reqId );
userPanel.setContent( ajaxEvent.responseText );
userPanel.show();
}

Create a custom event class that can handle this behavior and your
set.

Garrett
 
D

Daniel Loose

What you need to do is define an inner function in
wuwinoAjax and use it to handle readystatechange events. Make your
callback inside that function and pass a reference to the request
object. As the request object (and the callback function) will be
preserved in a closure that creates a circular reference, make sure
you set the onreadystatechange property of the request object to null
when the request finishes loading.

Thank you very much, and all posters! Sorry to ask again, but I'm
unexperienced in advanced javascript (often ending up losing hours to
code sth that's actually quite simple) - could you please give some
code example? Needs not be complete and working, just the main lines/
functionality would really help me out a lot. Thanx again and best
wishes to all!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top