Résumé
Twitter
Work Blog
Guestbook
Login

Foundations of AJAX

Share |
<= Previous Post | Next Post =>

I finished the book Foundations of AJAX by Apress today at lunch and I am at a loss. I thought I had an AJAX book in my hands and with respect to the authors, they did float around the topic through the entire book. Now, to be completely fair, I am very "worn out" by technology advancements in the past couple of years, so I am probably quick on the trigger...

Book Cover

The Review...

I usually judge a book by what I personally got out of it without attempting to consider what others may glean from the printed knowledge. In this case, I expected a plethora of AJAX gems scattered throughout the book. Instead, I got about 10 lines of AJAX and then I felt like I was being instructed on how to write, test, and debug JavaScript and utilize the DOM. A few examples of how to interact with the server were written in Java. But, the majority of the book was all about other tools for writing good JavaScript! What really got me excited and upset at the same time was that a variety of AJAX frameworks were introduced in the very last appendix, but that their summaries were even shorter than the verbose details of JavaScript debugging. I'm sorry... did I get a book on JavaScript debugging? I admit that debugging is a component of an AJAX solution, but let's touch on it and move on! (I did like the mention of the FireFox GreaseMonkey AJAX debugging script.) Finally, the ideas presented for actually using AJAX sucked. Yes, they sucked. In my opinion, the book read "Hi, here's AJAX in 10 lines of JavaScript over and over with slight example varations. Now, let's discuss the DOM in depth and all of the browser implementation nuances. Now, let's spend whole chapters on testing and debugging JavaScript. Then, let's remind the readers they're reading an AJAX book, but dive into cross-browser JavaScript. As an aside, we'll mention some really cool frameworks for developing AJAX solutions, but we won't spend much time talking about those, 'cause why would you be very interested in learning about using an existing framework? But, we'll suggest that you should take advantage of existing code libraries."

My Suggestion

My suggestion is this: Skip the book and read the following few lines. *The code below is for experienced JavaScript programmers. It is also rather verbose (keeping in touch with the book itself).

//a custom javascript class
var ajax = function() {
   //private variable using "var"
   var _http = null; //holds XmlHttpRequest object

   //IE instantiation
   if (window.ActiveXObject) {
      _http = new ActiveXObject("Microsoft.XMLHTTP");
   }
   //standard instantiation
   else if (window.XMLHttpRequest) {
      _http = new XMLHttpRequest();
   }

   //private method for making generic
   //method calls to the server
   var _doMethod = function(method, url, data, callback, asynchronous) {
      _http.open(method, url, asynchronous);
      _http.onreadystatechange = callback;
      _http.send(data);
   };

   //public GET server request
   this.doGet = function(url, callback, asynchronous) {
      _doMethod("GET", url, null, callback, asynchronous);
   };

   //public POST server request
   this.doPost = function(url, data, callback, asynchronous) {
      _doMethod("POST", url, data, callback, asynchronous);
   };

   //public HEAD server request
   this.ping = function(url, callback, asynchronous) {
      _doMethod("HEAD", url, null, callback, asynchronous);
   };

   //test if the server response is OK (HTTP_SUCCESS_OK)
   this.isResponseOK = function() {
      var ok = false; //not yet

      //ready state is not consistently implemented in all
      //browsers, but 4 ("Completed") is the final state
      if (_http.readyState == 4) {
        //HTTP_SUCCESS_OK
        if (_http.status == 200) {
           ok = true; //yea!
        }
      }

      return ok;
   };

   //get the server's response as TEXT
   this.getText = function() {
      var textResult = null;

      //only if the response is ok
      if (this.isResponseOK()) {
         textResult = _http.responseText;
      }

      return textResult;
   };

   //get the server's response as XML
   this.getXml = function() {
      var xmlResult = null;

      //only if the response is ok
      if (this.isResponseOK()) {
         xmlResult = _http.responseXml;
      }

      return xmlResult;
   };
};

//custom class for page-level functions
var page = {
   //initialize page calls
   //*in this case, just one call to "makeRequest"
   init : function() {
      this.makeRequest();
   },
   //internal AJAX demo object
   ajaxDemo : new ajax(),
   //makes a request using "ajaxDemo"
   makeRequest : function() {
      this.ajaxDemo.doGet('http://www.ericis.com/rss.aspx', page.handleResponse, true);
   },
   //handles the response from "ajaxDemo"
   handleResponse : function() {
      //if the response if ok
      if (page.ajaxDemo.isResponseOK()) {
         //alert the XML returned from the server
         var rss = page.ajaxDemo.getText();
         alert(rss);
      }
   }
};

//window load event (same as <body onload="page.init()">)
//assigns an anonymous function to call the page.init();
window.onload = function() {
   page.init();
};
Now, I just wrote that from the top of my head in less than 20 minutes, which should give you an idea about why #1 the authors struggled to find enough content to fill a book about AJAX and #2 why they did a poor job by spending so much time on JavaScript itself.

AJAX Client Frameworks

AJAX Server Frameworks

FireFox Extensions

*Comes bundled with a great DOM Inspector (under "Tools")

Other AJAX Stuff

Citations

Foundations of AJAX
Ryan Asleson & Nathaniel T. Schutta
ISBN: 1-59059-5823

Share |
<= Previous Post | Next Post =>

Comments

Submit New Comment

*Your e-mail is not shared with others. If provided, I simply use it as a method of contacting you about your comment(s) on this website. If you have a direct question, simply contact me.

*Cannot contain HTML and NO SPAM!