Can it be done like this (ajax)?

R

Ralph

I was playing a little bit with a lot of ajax frameworks. I have even
wrote one small by myself. But one thing still drives me crazy: Whenever
I'm doing an ajax call I need to specify handler to handle data coming
from a server script. That means that almost for every action I need two
functions.

Since I'm still new to all of this I'd like to ask how can i achieve
something like this:

function function_name(el) {
var i_need_this = get_what_i_need(el.id);
return _i_need_this;
}

where get_what_i_need function is returning some information using ajax
from a server.

Thank you
 
R

Ralph

David Golightly wrote:

Thank you very much That gave me some ideas how can i solve my problem.
Thanks again David!

Ralph
Since I'm still new to all of this I'd like to ask how can i achieve
something like this:

function function_name(el) {
var i_need_this = get_what_i_need(el.id);
return _i_need_this;
}

where get_what_i_need function is returning some information using ajax
from a server.

There are a couple of ways to do this. One is to turn of the
"asynchronous" option to XMLHttpRequest (async=false) - this will make
your script wait until the request is complete. However, most times
you don't want to do that, as server or connection problems could occur
and basically freeze your script. Also, it takes away the advantages
of async, which is that the user can go on and do something else while
waiting for the response. Instead, to use async, you have to change
your thinking from writing function-driven code to writing event-driven
code. This means code like the above isn't really the way to go.
Instead, you're going to have breaks in your code, like this:

function makeRequest(el) {
// whatever framework you're using here
// let's just say AjaxRequest(url, method, async, data, callback
new AjaxRequest(url, method, true, el.id, doSomethingWithResult);
}

function doSomethingWithResult(result) {
// takes charge after response comes back
}

If you want to preserve information across the divide you can save it
to an object:

var requests = [];

function makeRequest(el) {
...
requests.push({el: el, otherstuff:mystuff});
data = el.id + ',' + (requests.length-1)
...// make request
}

function doSomethingWithResult(result) {
// takes charge after response comes back
// your server script will have to pass back the request ID
somewhere in the result as index to requests array
}

With an average connection it will take a while, a second or so
perhaps, so you just have to design around that if you want to write
responsive Ajax apps.

-David
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top