5.2. The RequestManager Object

The RequestManager object is the main object used for handling XHR requests. Its main job is to manage two simultaneous XHR requests, since no more than two can be sent on any client that obeys the HTTP 1.1 specification. This object handles the creation and destruction of all XHR objects used to make the requests, meaning that the developer never has to worry about creating an XHR object directly. Additionally, the RequestManager object handles the monitoring of all requests and the marshaling of results to particular event handlers.

Since requests are metered by connections that the client is making, the RequestManager is implemented using the singleton pattern (meaning that only one instance can be created per page). It wouldn't make sense to allow more than one instance to be created, since there's only ever two available requests for an entire page (for example, it wouldn't make sense to create three RequestManager objects because there are still only two requests to manage). The basic pattern used to define this object is:

var RequestManager = (function () {

    var oManager = {
        //properties/methods go here
    };

    //initialization goes here

    //return the object
    return oManager;

})();

This is one of several ways to implement a singleton pattern in JavaScript. The outermost function is anonymous and is called immediately as the code is executed, creating the object, initializing it, and returning it. In this way RequestManager becomes a globally available ...

Get Professional Ajax, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.