473,395 Members | 1,936 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,395 software developers and data experts.

Async XMLHttpRequest and class variables

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

Dec 13 '05 #1
6 1832
ga*************@gmail.com 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
Dec 13 '05 #2
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:
ga*************@gmail.com 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


Dec 13 '05 #3
ga*************@gmail.com 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
Dec 14 '05 #4
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.8), 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>
Dec 14 '05 #5
Thomas 'PointedEars' Lahn wrote:
ga*************@gmail.com 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

Dec 14 '05 #6
ga*************@gmail.com 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
Dec 14 '05 #7

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

Similar topics

8
by: Dave | last post by:
I'm using the BeginInvoke method of a delegate to invoke a thread asynchronously and then use the EndInvoke to retrieve the value. This works wonderfully until a Serviced Component is added to...
12
by: knocte | last post by:
Hello. I have always thought that the eval() function was very flexible and useful. If I use it, I can define functions at runtime!! However, I have found a case where eval() does not work...
10
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginX and EndX, plus the regular X syncronous method. Delegates seemed like the way to go, however, I still am having problems getting...
2
by: Anand | last post by:
Hi Season Greetings, I'm trying to implement client call backs in my page. i'm using user controls (composite controls) and these are placed in my master page. I'm implementing...
6
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to...
1
by: 1388-2/HB | last post by:
I have a class file in my App_Code folder called "clsBasePage.vb" that looks like this: Public Class clsBasePage Inherits System.Web.UI.Page ...stuff... End Class It's a "page" in that it...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.