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. 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...

2. 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...

3. 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 ...

4. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...

5. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...

6. Advantage of parameterless functions in JavaScript
Using Parameterless Functions Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at...

7. JavaScripot Functions
Treating Functions Like Objects Problem You want to take advantage of the fact that functions are objects (remember, everything is an object in JavaScript). Theory Many people think that a function is some keyword used in JavaScript. A function is also an object that can be manipulated. Knowing that a function is an object makes it very interesting from the perspective of writing JavaScript code, because the code can treat the function like another other object. This mean...