By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,730 Members | 1,567 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,730 IT Pros & Developers. It's quick & easy.

variables and ajax

P: n/a
I'm working on a "simple" tracking system for a client that will be
receiving elearning packages from 3rd parties.

I'm intending to do the communication with the database using ajax.

My problem is one of concepts. An elearning package, of which I have no
control over, might have code that looks like this;

1: var val = GetValue("name");
2:
3: if (val) {
4: doThis();
5: } else {
6: doThat();
7: }

Line 1 would trigger an ajax call to the db but as the call is
asynchronous does that mean that at line 3 "val" may be undefined?

I'm still struggling with ajax concepts. I can understand how to, say,
update a database but simple things like getting a value into a variable
is escaping me.

Andrew Poulos
Nov 9 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Andrew Poulos meinte:
I'm working on a "simple" tracking system for a client that will be
receiving elearning packages from 3rd parties.

I'm intending to do the communication with the database using ajax.

My problem is one of concepts. An elearning package, of which I have no
control over, might have code that looks like this;

1: var val = GetValue("name");
2:
3: if (val) {
4: doThis();
5: } else {
6: doThat();
7: }

Line 1 would trigger an ajax call to the db but as the call is
asynchronous does that mean that at line 3 "val" may be undefined?
Yes. GetValue() (BTW: better name it "getValue" - it's no constructor)
sends the request. End of story for this part of the script. Once the
response is being returned a callback function assigned to the
onreadystatechange property of the XHR-Object is triggered. This
callback function can now decide whether val is set and in turn invoke
doThis() or doThat(). It's explained in detail an (in)numerous websites
- here's one:
https://developer.mozilla.org/En/Using_XMLHttpRequest
I'm still struggling with ajax concepts. I can understand how to, say,
update a database but simple things like getting a value into a variable
is escaping me.
In the first case you do not necessarily need a callback function. In
the latter case you do.

Gregor
Nov 9 '08 #2

P: n/a
Gregor Kofler wrote:
Andrew Poulos meinte:
>I'm working on a "simple" tracking system for a client that will be
receiving elearning packages from 3rd parties.

I'm intending to do the communication with the database using ajax.

My problem is one of concepts. An elearning package, of which I have
no control over, might have code that looks like this;

1: var val = GetValue("name");
2:
3: if (val) {
4: doThis();
5: } else {
6: doThat();
7: }

Line 1 would trigger an ajax call to the db but as the call is
asynchronous does that mean that at line 3 "val" may be undefined?

Yes. GetValue() (BTW: better name it "getValue" - it's no constructor)
sends the request. End of story for this part of the script. Once the
response is being returned a callback function assigned to the
onreadystatechange property of the XHR-Object is triggered. This
callback function can now decide whether val is set and in turn invoke
doThis() or doThat(). It's explained in detail an (in)numerous websites
- here's one:
https://developer.mozilla.org/En/Using_XMLHttpRequest
Thanks, I follow what you're saying but I get lost with how the callback
function knows which variable to assign a value to. Would I need to hard
code the variable name into the callback?

Andrew Poulos
Nov 9 '08 #3

P: n/a
Andrew Poulos meinte:
Thanks, I follow what you're saying but I get lost with how the callback
function knows which variable to assign a value to. Would I need to hard
code the variable name into the callback?
What do you mean by "hard code". A closure is all you need.

var foo;
....

xhr.onreadystatechange = function() {
...
foo = xhr.responseText;
}

....

Gregor
Nov 10 '08 #4

P: n/a
Gregor Kofler wrote:
Andrew Poulos meinte:
>Thanks, I follow what you're saying but I get lost with how the
callback function knows which variable to assign a value to. Would I
need to hard code the variable name into the callback?

What do you mean by "hard code". A closure is all you need.

var foo;
...

xhr.onreadystatechange = function() {
...
foo = xhr.responseText;
}
I don't know what the names of the variable(s) that a 3rd party
elearning package may use. So I can't write
foo = xhr.responseText;
as the variable name could be anything.

Andrew Poulos
Nov 10 '08 #5

P: n/a
Andrew Poulos wrote:
Gregor Kofler wrote:
>Andrew Poulos meinte:
>>Thanks, I follow what you're saying but I get lost with how the
callback function knows which variable to assign a value to. Would I
need to hard code the variable name into the callback?

What do you mean by "hard code". A closure is all you need.

var foo;
...

xhr.onreadystatechange = function() {
...
foo = xhr.responseText;
}

I don't know what the names of the variable(s) that a 3rd party
elearning package may use. So I can't write
foo = xhr.responseText;
as the variable name could be anything.
Would a synchronous call be appropriate in this situation? That is:

var foo = getValue("name");

function getValue(n) {
// ajax stuff here
...
xhr.open("post","test.php", false);
xhr.send(passData);
return xhr.responseText:
...
}

Andrew Poulos
Nov 10 '08 #6

P: n/a
On Nov 10, 8:14 am, Andrew Poulos <ap_p...@hotmail.comwrote:
Gregor Kofler wrote:
Andrew Poulos meinte:
Thanks, I follow what you're saying but I get lost with how the
callback function knows which variable to assign a value to. Would I
need to hard code the variable name into the callback?
What do you mean by "hard code". A closure is all you need.
var foo;
...
xhr.onreadystatechange = function() {
...
foo = xhr.responseText;
}

I don't know what the names of the variable(s) that a 3rd party
elearning package may use. So I can't write
foo = xhr.responseText;
as the variable name could be anything.
Specify the API as expecting a callback:

// API will pass value as first parameter of callback:
getValue(identifier,callback);

// call it like:

getValue("name", function (foo) {
// 3rd party developer does what he wants to do with foo in here
});

// alternatively, can also be called like:

function myCallback (x) {
// do stuff with x here
}
getValue("name",myCallback);

/*
A possible implementation of getValue is something like:
*/

function getValue(n,fn) {
xhr = new XMLHttpRequest();
...

xhr.onreadystatechange = function () {
...

fn(xhr.responseText);
}
}
Nov 10 '08 #7

P: n/a
Andrew Poulos wrote:
Would a synchronous call be appropriate in this situation? That is:

var foo = getValue("name");

function getValue(n) {
// ajax stuff here
...
xhr.open("post","test.php", false);
xhr.send(passData);
return xhr.responseText:
...
}
Synchronous request-response handling would be the only viable option with
this approach. Whether that approach would be appropriate here is quite a
different matter, though; it would depend on the kind of request, the
responsiveness that can be expected of the server, and the responsiveness
demanded from the application. Synchronous request-response handling has a
strong tendency to suspend all other threads of the user agent while it is
in progress.
HTH

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 10 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.