Implementing the Synchronous Class in Ajax

an article added by: Sonja Lande at 06012007


In: Root » » AJAX » Implementing the Synchronous Class in Ajax

French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic

Another potential solution is to use the synchronous version of XMLHttpRequest. For the most part, I use asynchronous mode, but there are times when synchronous mode is more appropriate. article 1-3 presents an example in which the test cannot continue until the Ajax page has finished loading its content. To implement a Synchronous version of Asynchronous, only the call method needs to be adapted everything else can remain as is. For example, to convert the initial client code to use synchronous mode, you could use the following code:

   function LoadAtomFeed() {
   var synchronous = new Synchronous();
   synchronous.settings = {
   onComplete : function(xmlhttp) {
   parseAtom( xmlhttp.responseXML);
   flexbox.update();
   }
   }
   synchronous.get("/services/blog/entries/current");
 }

The bold code shows the only major change: instead of instantiating Asynchronous, it instantiates Synchronous. You still provide a settings code block, and you still call the get function. The reason this approach works is due to the way that Asynchronous and Synchronous are wired together, as illustrated by the following code:

   function Asynchronous(userSettings) {
   this.xmlhttp = new FactoryXMLHttpRequest();
   this.isBusy = false;
   this.userSettings = userSettings;
   }
   Asynchronous.prototype.get = HttpRequest_get;
   Asynchronous.prototype.put = HttpRequest_put;
   Asynchronous.prototype.del =  HttpRequest_delete;
   Asynchronous.prototype.post = HttpRequest_post;
   Asynchronous.prototype.call = Asynchronous_call;
   function Synchronous(userSettings) {
   this.xmlhttp = new FactoryXMLHttpRequest();
   this.isBusy = false;
   this.userSettings = userSettings;
   }
   Synchronous.prototype.get = HttpRequest_get;
   Synchronous.prototype.put = HttpRequest_put;
   Synchronous.prototype.del = HttpRequest_delete;
   Synchronous.prototype.post = HttpRequest_post;
 Synchronous.prototype.call = Synchronous  call;

The bold code shows the single change in each class. Notice that this way of defining a class uses a contract. Each type has five functions: get, put, del, post, and call. The four functions get, put, del, and post could be considered a module and thought of as a mixin. Each of the four functions adds value in some way, but relies on a contract where the type contains a call method. The call method is what varies on the Asynchronous and Synchronous types, as each has a unique implementation. The synchronous call implementation is as follows.

   Source: /client/scripts/jaxson/communications.js
   function Synchronous_call(request) {
   var instance = this;
   this.xmlhttp.open(request.action, request.url,  false,
   this.settings.username,  this.settings.password);
   if (request.headers) {
   for( defHeader in request.headers) {
   this.xmlhttp.setRequestHeader(defHeader,  request.headers[defHeader]);
   }
   }
   if (this.settings.headers) {
   for( defHeader in this.settings.headers) {
   this.xmlhttp.setRequestHeader(defHeader,
   this.settings.headers[defHeader]);
   }
   }
   try {
   this.xmlhttp.send(request.data);
   if (instance.settings.onComplete) {
   instance.settings.onComplete(instance.xmlhttp);
   }
   }
   catch( e) {
   globals.errorHandler(e);
   }
 }

Without going into too much detail, you can see that the implementations are very similar. The only real difference is that calling onComplete after calling the send method mimics the asynchronous calling of the onComplete code block. In synchronous mode, after the send method returns, the state of XMLHttpRequest is valid and calling onComplete is correct.

legal disclaimer

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

related articles

1. Ajax
Given the recent interest in Ajax, you’d be forgiven for thinking it was a new technology. In fact, the XMLHttpRequest object has been around for years. In technical terms, asynchronous JavaScript interaction with the server is nothing new. All of the other elements of the Ajax model have also been around for quite some time: CSS, (X)HTML, and DOM Scripting. Yet in 2005, interest in this methodology soared. Could it really be that simply giving this approach a snappy name like Ajax was responsible for the sudde...

2. Understanding the Definition and Philosophy of Ajax
The focus of this article is to provide solutions to some common, general problems and questions that are bound to arise before or during development of Asynchronous JavaScript and XML (Ajax) and Representational State Transfer (REST) applications. These common questions are not always technical in nature, often leaning more toward theory or philosophy of development. The problem with these kinds of questions is that once you begin to think about them, you keep going in a circle and end up where you star...

3. Understanding the Definition and Philosophy of Web Services and SOA
Understanding the Definition and Philosophy of Web Services and SOA Wikipedia offers the following definition of Web services:4 The W3C defines aWeb service as a software system designed to support interoperable machine-to-machine interaction over a network. This definition encompasses many different systems, but in common usage the term refers to those services that use SOAPformatted XML envelopes and have their interfaces described by WSDL. For ex...

4. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...

5. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...

6. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...

7. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...