...

View Full Version : oop help



b_hole
10-23-2006, 06:08 PM
Hello,

I'm starting to write a set of objects and function to handle easily XMLHttpRequests. The thing is I've just started learning OOP...
Here's what I wrote so far:

var myAJAX={
requestXML:function() {
if (window.XMLHttpRequest) {return new XMLHttpRequest();}
else if (window.ActiveXObject) {
try {return new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {
try {return new ActiveXObject("Microsoft.XMLHTTP");}
catch (e) {}
}
}
else return false;
},
getXML:function(url) {
http_request=new ajax.request();
http_request.onreadystatechange=function() {handleXML("myID", this)};
http_request.open("GET", url, true);
http_request.send(null);
},
postXML:function(url) {
http_request=new ajax.request()
http_request.onreadystatechange=function() {};
http_request.open("POST", url, true);
http_request.send(null);
}
}Do you think this code is OK? What would you change\add?
BTW, what is the "e" inside every catch?

A1ien51
10-23-2006, 06:29 PM
the exception...read about it here: http://www.w3schools.com/js/js_try_catch.asp

Eric

A1ien51
10-23-2006, 06:30 PM
open should come be onreadystatechange....

Eric

A1ien51
10-23-2006, 06:32 PM
headers should be set for a post

should be a check if the object is supported.

There should be a spot for errors and so on.

OI would say look into prototype.js and its Ajax finctionality and don't try to write your own!

Eric

b_hole
10-23-2006, 06:53 PM
Thanks for the comments.


headers should be set for a postExample?

should be a check if the object is supported.You mean I should add an if(http_request) statement?

There should be a spot for errors and so on.I'm not sure I understand, try&catch do that, no?


OI would say look into prototype.js and its Ajax finctionality and don't try to write your own!That's exactly what I want to avoid :) I want to write my own script, and learn from it. I tried learning from reading prototype.js, but it's too complex for my knowledge. I'm sure I'll have problems, but I think that way I'll understand - using my experience.

Any other tips?

b_hole
10-24-2006, 12:10 PM
Another question on that issue:
I'm getting data from a XML file, and moving the content into a var:

innerhtml="";
for (i=0; i<xmldoc.getElementsByTagName("r1").length; i++) {
root_movie=xmldoc.getElementsByTagName("r2").item(i);
root_score=xmldoc.getElementsByTagName("r3").item(i);
root_year=xmldoc.getElementsByTagName("year").item(i);
innerhtml+="<tr><td>"+root_movie.firstChild.data+"</td><td>"+root_score.firstChild.data+"</td><td>"+root_year.firstChild.data+"</td></tr>";
}Now I just throw innerhtml into an element innerHTML:

document.getElementById("mytable").innerHTML=innerhtml;

I'm sure there's a better way other then innerHTML, but what?

Kor
10-24-2006, 01:39 PM
Yes on using DOM



var root=document.getElementById("mytable").getElementsByTagName('tbody')[0];
var myR=xmldoc.documentElement.getElementsByTagName('r1');
var R=[
xmldoc.documentElement.getElementsByTagName('r2'),
xmldoc.documentElement.getElementsByTagName('r3'),
xmldoc.documentElement.getElementsByTagName('year')
]
var i=0, r
while(r=myR[i++]){
var oTR=document.createElement('tr');
for(var j=0;j<R.length;j++){
var oTD=document.createElement('td');
oTD.appendChild(document.createTextNode(R[j][i-1].firstChild.data));
oTR.appendChild(oTD)
}
root.appendChild(oTR)
}

b_hole
10-24-2006, 02:19 PM
First - what's that?

var i=0, r


Second, it's not working. When removing the ".documentElement" I get only the first TR.
I don't get any error in Firefox's error console, so I don't know where to look.
here's the structure of my XML file:

<?xml version="1.0" encoding="utf-8" ?>
<movies>
<movie>
<title>movie title 1</title>
<score>1-10</score>
<year>XXXX</year>
</movie>
<movie>
<title>movie title 2</title>
<score>1-10</score>
<year>XXXX</year>
</movie>
</movies>Here's the code you wrote:

var xmldoc=httprequest.responseXML;
var root=document.getElementById("movies");
var myR=xmldoc.getElementsByTagName("movies");
var R=[xmldoc.getElementsByTagName("title"), xmldoc.getElementsByTagName("score"), xmldoc.getElementsByTagName("year")];
var i=0, r;
while (r=myR[i++]) {
var oTR=document.createElement("tr");
for (var j=0; j<R.length; j++) {
var oTD=document.createElement("td");
oTD.appendChild(document.createTextNode(R[j][i-1].firstChild.data));
oTR.appendChild(oTD)
}
root.appendChild(oTR);
}

Kor
10-24-2006, 02:40 PM
That was not the code I have written. Anyway, Now that I see your XML, try this:


var root=document.getElementById('mytable').getElementsByTagName('tbody')[0];
var R=[
xmldoc.getElementsByTagName('title'),
xmldoc.getElementsByTagName('score'),
xmldoc.getElementsByTagName('year')
]
for(var i=0;i<R[0].length;i++){
var j=0, oTR=document.createElement('tr'), r;
while(r=R[j++]){
var oTD=document.createElement('td');
oTD.appendChild(document.createTextNode(r[i].firstChild.data));
oTR.appendChild(oTD);
}
root.appendChild(oTR);
}


and make sure that you have a table like



<table id="mytable">
</table>

Kor
10-24-2006, 02:41 PM
var i=0, r;

is a short notation for


var i=0;
var r;

b_hole
10-24-2006, 02:56 PM
var i=0, r;

is a short notation for


var i=0;
var r;
Nice, thanks.


and make sure that you have a table like

<table id="mytable"> </table>Even better, I have a tbody with an ID :)
Thanks for the help, it's working properly now. That's a very elegant code :cool:


Another small question: I apply an onload function for clicking an a TH element:
onload=function() {
var ths=document.getElementsByTagName("th");
for (object in ths) {
ths[object].onclick=function() {
alert();
}
}
}How can I alert the TH number (something like thd[i]) in the DOM?

Kor
10-24-2006, 03:34 PM
In this case you should treat the collection like an ordered array, not like an object (which is an unordered array, that means it has neither length nor indexes)


onload=function(){
var ths=document.getElementsByTagName("th");
for(var i=0;i<ths.length;i++){
ths[i].ind=i;//creates a non-native property for the object
ths[i].onclick=function(){alert(this.ind)}
}
}

b_hole
10-24-2006, 04:02 PM
Thanks, Kor :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum