AJAX Thoughts, Suggestions and Examples Part II
Suggestion: Create a ajax.js file which will contain classes of which instances can be
created. This serves a few benefits including code reusability, saved network bandwidth due to reused code,
request queueing and allowing multiple connections. An viable implementation would be to have two classes,
a request class and a queue class. By creating a new instance of the queue
class, a queue is created in which requests can be added. As a request or requests are added to
the queue, they are processed synchronously in order. To process multiple requests at the same time,
multiple queues can be created and requests added to them as appropriate. Here is an example, note
this is currently used to connect to Microsoft IIS, it however should work for other web servers:
This function can also be used for code reusability.
Now we just need to instantiate our classes and make a request. This can now be done
by a few lines of code. Below is an example. The first function makes the request. The second
function is called back from our AJAXQueue after the reply from the web server has been returned.
Explore more in AJAX Thoughts, Suggestions and Examples Part III here