Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13

Thread: oop help

  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts

    oop help

    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:
    Code:
    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?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    the exception...read about it here: http://www.w3schools.com/js/js_try_catch.asp

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    open should come be onreadystatechange....

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the comments.

    Quote Originally Posted by A1ien51 View Post
    headers should be set for a post
    Example?
    Quote Originally Posted by A1ien51 View Post
    should be a check if the object is supported.
    You mean I should add an if(http_request) statement?
    Quote Originally Posted by A1ien51 View Post
    There should be a spot for errors and so on.
    I'm not sure I understand, try&catch do that, no?

    Quote Originally Posted by A1ien51 View Post
    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?

  • #6
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another question on that issue:
    I'm getting data from a XML file, and moving the content into a var:
    Code:
    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:
    Code:
    document.getElementById("mytable").innerHTML=innerhtml;
    I'm sure there's a better way other then innerHTML, but what?

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yes on using DOM

    Code:
    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)
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First - what's that?
    Code:
    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:
    Code:
    <?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:
    Code:
    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);
    }

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    That was not the code I have written. Anyway, Now that I see your XML, try this:
    Code:
    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

    Code:
    <table id="mytable">
    </table>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Code:
    var i=0, r;
    is a short notation for
    Code:
    var i=0;
    var r;
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    Code:
    var i=0, r;
    is a short notation for
    Code:
    var i=0;
    var r;
    Nice, thanks.

    Quote Originally Posted by Kor View Post
    and make sure that you have a table like
    Code:
    <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


    Another small question: I apply an onload function for clicking an a TH element:
    Code:
    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?

  • #12
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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)
    Code:
    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)}
    }
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #13
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, Kor


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •