Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Async XMLHttpRequest and class variables

Reply
Thread Tools

Async XMLHttpRequest and class variables

 
 
gabriel.landais@gmail.com
Guest
Posts: n/a
 
      12-13-2005
Hi,
I'm currently building a parser class in JS and I have a question
about variables. I retrieve XML data and then process it. After that, I
process a result array (mydataarray). It looks like this :

function MyClass()
{

var mydataarray = [];

this.processXMLnode = processXMLnode;
this.processxmlDoc = processxmlDoc;
this.processrequest = processrequest;

function processXMLnode(marker) {
// ADD SOMETHING TO mydataarray
}

function processxmlDoc(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data" );
mydataarray = [];

for (var i = 0; i < markers.length; i++) {
processXMLnode(markers[i]);
}

// PROCESS mydataarray
}

function processrequest(url, async) {
var request = XMLHttpRequest.create();

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

}

I'm not a JS expert, does processxmlDoc will be executed from the same
thread (or execution context) every time? Does processxmlDoc is called
only when nothing else is executing? Otherwise, how can I use a safe
array?

Cheers
Gabriel

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I'm currently building a parser class in JS


The below is not a class declaration, instead a function declaration
and probably a constructor. You are not using any of the class-based
OOP features of the latest JavaScript/JScript/ECMAScript versions/editions,
instead you are using inner functions and probably the prototype-based
OOP features (with `new FunctionObjectReference()').

> and I have a question about variables. I retrieve XML data and then
> process it. After that, I process a result array (mydataarray). It
> looks like this :
>
> function MyClass()
> {
> [...]
>
> function processxmlDoc(xmlDoc) {
> var markers =
> xmlDoc.documentElement.getElementsByTagName("data" );
> mydataarray = [];
>
> for (var i = 0; i < markers.length; i++) {
> processXMLnode(markers[i]);
> }
>
> // PROCESS mydataarray
> }
>
> function processrequest(url, async) {
> var request = XMLHttpRequest.create();
>
> request.open("GET", url, async);
>
> if (async) {
> request.onreadystatechange = function() {
> if (request.readyState == 4) {
> processxmlDoc(request.responseXML);
> }
> }
> request.send(null);
> }
> else
> {
> request.send(null);
> processxmlDoc(request.responseXML);
> }
> }
>
> }
>
> I'm not a JS expert, does processxmlDoc will be executed from the
> same thread (or execution context) every time?


No. The use of `async' here implies a value that can be type-converted
to boolean. Therefore, if the value of `async' is a true-value, the
event listener is assigned. It it, processxmlDoc() is called iff
request.readyState equals 4 (success) when the `readystatechange' event
occurs. If, on the other hand, the value of `async' is a false-value,
processxmlDoc() is always called [after request.send(null)].

> Does processxmlDoc is called only when nothing else is executing?


Yes, the respective programming languages are single-threaded.
The inevitable delay in processing is mitigated, but not completely
compensated by the use of event handlers for host objects.

> Otherwise, how can I use a safe array?


It is already safe as it is.


PointedEars
 
Reply With Quote
 
 
 
 
gabriel.landais@gmail.com
Guest
Posts: n/a
 
      12-13-2005
I understand what I've done, hopefully In fact I've created that
pseudo class to access inner functions to be able to do something like
:

function a() {
b();
}

function b() {
c();
}

function c() {
a();
}

With some "if" of course

Understood for the single thread context.
Cheers!

Thomas 'PointedEars' Lahn wrote:
> (E-Mail Removed) wrote:
>
> > I'm currently building a parser class in JS

>
> The below is not a class declaration, instead a function declaration
> and probably a constructor. You are not using any of the class-based
> OOP features of the latest JavaScript/JScript/ECMAScript versions/editions,
> instead you are using inner functions and probably the prototype-based
> OOP features (with `new FunctionObjectReference()').
>
> > and I have a question about variables. I retrieve XML data and then
> > process it. After that, I process a result array (mydataarray). It
> > looks like this :
> >
> > function MyClass()
> > {
> > [...]
> >
> > function processxmlDoc(xmlDoc) {
> > var markers =
> > xmlDoc.documentElement.getElementsByTagName("data" );
> > mydataarray = [];
> >
> > for (var i = 0; i < markers.length; i++) {
> > processXMLnode(markers[i]);
> > }
> >
> > // PROCESS mydataarray
> > }
> >
> > function processrequest(url, async) {
> > var request = XMLHttpRequest.create();
> >
> > request.open("GET", url, async);
> >
> > if (async) {
> > request.onreadystatechange = function() {
> > if (request.readyState == 4) {
> > processxmlDoc(request.responseXML);
> > }
> > }
> > request.send(null);
> > }
> > else
> > {
> > request.send(null);
> > processxmlDoc(request.responseXML);
> > }
> > }
> >
> > }
> >
> > I'm not a JS expert, does processxmlDoc will be executed from the
> > same thread (or execution context) every time?

>
> No. The use of `async' here implies a value that can be type-converted
> to boolean. Therefore, if the value of `async' is a true-value, the
> event listener is assigned. It it, processxmlDoc() is called iff
> request.readyState equals 4 (success) when the `readystatechange' event
> occurs. If, on the other hand, the value of `async' is a false-value,
> processxmlDoc() is always called [after request.send(null)].
>
> > Does processxmlDoc is called only when nothing else is executing?

>
> Yes, the respective programming languages are single-threaded.
> The inevitable delay in processing is mitigated, but not completely
> compensated by the use of event handlers for host objects.
>
> > Otherwise, how can I use a safe array?

>
> It is already safe as it is.
>
>
> PointedEars


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-14-2005
(E-Mail Removed) wrote:

> I understand what I've done, hopefully In fact I've created that
> pseudo class to access inner functions to be able to do something like
> :
>
> function a() {
> b();
> }
>
> function b() {
> c();
> }
>
> function c() {
> a();
> }
>
> With some "if" of course


Now that is real nonsense.

Say I call a(), it calls b(), which calls c(), which calls a(), ...
Say I call b(), it calls c(), which calls a(), which calls b(), ...
Say I call c(), it calls a(), which calls b(), which calls c(), ...

And there is no inner function whatsoever.

If the above is instead but a bad example and you assumed that you
would be able to call b.c() as in

function b()
{
function c()
{
// ...
}
}

then this is not entirely true. What would be used then is a _JavaScript_
(Mozilla/2+) extension to ECMAScript, it is not available in JScript (IE)
or Opera, for example.

One of many correct ways to implement a public method is

function Foo()
{
this.bar = function()
{
// ...
}
}

var a = new Foo();
a.bar();

See
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide#Details_of_the_Object_Mo del>

> Understood for the single thread context.


At least.

> [Full quote]


Do not top-post on Usenet.

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>


PointedEars
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-14-2005
Thomas 'PointedEars' Lahn wrote:

> If [...] you assumed that you would be able to call b.c() as in
>
> function b()
> {
> function c()
> {
> // ...
> }
> }
>
> then this is not entirely true. What would be used then is a _JavaScript_
> (Mozilla/2+) extension to ECMAScript, it is not available in JScript (IE)
> or Opera, for example.


Example: function x() { function y() { alert("42"); } }; x.y() // 42 or not

It turns out that this feature is no longer available in JavaScript 1.6
as implemented in Firefox 1.5 (Mozilla/5.0 rv:1., while it still is in
JavaScript 1.5 as implemented in Mozilla/5.0 rv:1.7.12. The Support
Matrix[1] is growing larger every day


PointedEars
___________
[1] <URL:http://PointedEars.de/scripts/js-version-info>
 
Reply With Quote
 
gabriel.landais@gmail.com
Guest
Posts: n/a
 
      12-14-2005
Thomas 'PointedEars' Lahn wrote:
> (E-Mail Removed) wrote:
>
> > I understand what I've done, hopefully In fact I've created that
> > pseudo class to access inner functions to be able to do something like
> > :
> >
> > function a() {
> > b();
> > }
> >
> > function b() {
> > c();
> > }
> >
> > function c() {
> > a();
> > }
> >
> > With some "if" of course

>
> Now that is real nonsense.


Ok my scheme was really simplified here, should I put :

function doSomething() {
processrequest('backtrack.php?button='+this.someth ing, true);
}

function processXMLnode(marker) {
element = createNewButton(marker);
element.onclick = doSomething();
}

function processxmlDoc(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data" );
mydataarray = [];

for (var i = 0; i < markers.length; i++) {
processXMLnode(markers[i]);
}
}

function processrequest(url, async) {
var request = XMLHttpRequest.create();

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

>
> See
> <URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide#Details_of_the_Object_Mo del>
>


Thanks. I still believe that "prototype" thingy is oversized for my
problem.

> Do not top-post on Usenet.
>
> <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
>

Sorry about that!

My very first draft is there :
http://gabriel.landais.org/testGM/map.inc.js . It doesn't work very
well with IE Thanks to your site, I hope I will be able to
understand why!

Cheers

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-14-2005
(E-Mail Removed) wrote:

> Ok my scheme was really simplified here, should I put :
>
> [...]


Looks OK, but there are no inner functions whatsoever left

Or did you mean that you used that inside a function in order
to avoid spoiling the global namespace? Then I'll agree.

>> See
>>

<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide#Details_of_the_Object_Mo del>
>>

>
> Thanks. I still believe that "prototype" thingy is oversized for my
> problem.


If you create only one object with calling new MapClass(), then yes.
If not, you should consider the prototype since inheriting it from the
prototype as in

function Map()
{
// ..
}

Map.prototype.processXMLnode = function()
{
// ...
}

prevents each Map object to have a method (Function object) of its own
(initially) which will save you heap; on the other hand, it removes the
possible advantage of closures to define methods specific to an object
on initialization.

> My very first draft is there :
> http://gabriel.landais.org/testGM/map.inc.js .


Ahh, that approach is OK then. A bit old-fashioned IMHO, as function
expressions allow for

this.geturlproperties = function()
{
// ...
}

instead of

this.geturlproperties = geturlproperties;

function geturlproperties()
{
// ...
}

since JavaScript 1.3/JScript 2.0?/ECMAScript 3.

> It doesn't work very well with IE Thanks to your site, I hope I
> will be able to understand why!


Hmmm ... getAttribute() is often buggy and may not be necessary.

For responseXML to work in IE (with MSXML), you have to make sure
that the response is served at least with Content-Type: text/xml.

<URL:http://msdn.microsoft.com/library/en-us/xmlsdk/html/ab1b76cf-7dd9-46b5-b782-cc04823df117.asp>

Apart from that, I do not see syntax or semantical errors, but
I may have overlooked some, especially those that may be in the
constructors you call that are not defined in map.inc.js.


HTH

PointedEars
 
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
scope of local variables across async method calls with anon functions John Davison Javascript 1 03-06-2007 05:25 PM
Passing Variables through different functions for an XMLHttpRequest script. kasper48 Javascript 2 10-21-2006 04:43 AM
Newbie: async mode dedicated versus async mode interactive!! Pink_Floyd Cisco 4 06-16-2006 12:16 AM
Async webservice call in async webpage (.Net 2.0) does not return Steven ASP .Net Web Services 0 11-30-2005 01:06 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments