五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > XHR工廠

XHR工廠

文章來(lái)源:365jz.com     點(diǎn)擊數(shù):273    更新時(shí)間:2009-09-14 10:29   參與評(píng)論

 

/* XHR工廠 */

/* AjaxHandler interface. */

var AjaxHandler = new Interface('AjaxHandler', ['request', 'createXhrObject']);

/* SimpleHandler class. */

var SimpleHandler = function() {}; //implements AjaxHandler
SimpleHandler.prototype = {
    request: function(method, url, callback, postVars) {
        var xhr = this.createXhrObject();
        xhr.onreadystatechange = function() {
            if (xhr.readyState !== 4) {
                return;
            }
            (xhr.status === 200) ? callback.success(xhr.responseText, xhr.responseXML) : callback.failure(xhr.status);
        };
        xhr.open(method, url, true);
        if (method !== 'POST') {
            postVars = null;
        }
        xhr.send(postVars);
    },
    createXhrObject: function() { //Factory method.
        var methods = [
            function() { return new XMLHttpReques();},
            function() { return new ActiveXObject('Msxml2.XMLHTTP');},
            function() { return new ActiveXObject('Microsoft.XMLHTTP');}
        ];
       
        for(var i = 0, len = methods.length; i < len; i++){
            try{
                methods[i]();
            }catch(e){
                continue;
            }
            // If we reach this point, method[i] worked.
            this.createXhrObject = methods[i]; // Memoize the method.
            throw new Error('SimpleHandler: Could not create an XHR object.');
        }
    }
};

//var myHandler = new SimpleHandler();
//var callback = {
//    success: function(responseText) { alert('Successs: '+ responseText); },
//    failure: function(statusCode) { alert('Failure: '+ statusCode); }
//};

//myHandler.request('GET', 'sctipt.php', callback);


/* QueuedHandler class. */
var QueuedHandler = function() { //implement AjaxHandler
    this.queue = [];
    this.requestInProgress = false;
    this.retryDelay = 5; //In seconds. 
};

extend(QueuedHandler, SimpleHandler);

QueuedHandler.prototype.request = function(method, url, callback, postVars, override) {
    if (this.requestInProgress && !override) {
        this.queue.push({
            method: method,
            url: url,
            callback: callback,
            postVars: postVars
        });
    }else{
        this.requestInProgress =true;
        var xhr = this.createXhrObject();
        var that = this;
        xhr.onreadystatechange = function(){
            if (xhr.readyStatue !== 4) return;
            if (xhr.status === 200) {
                callback.success(xhr.responseText, xhr.responseXML);
                that.advanceQueue();
            }else{
                callback.failure(xhr.status);
                setTimeout(function() { that.request(method, url, callback, postVars, true); }, that.retryDelay * 1000);
            }
        };
        xhr.open(method, url, true);
        if (method !== 'POST') {
            postVars = null;
        }
        xhr.open();
    }
};

QueuedHandler.prototype.advanceQueue = function() {
    if (this.queue.length === 0) {
        this.requestInProgress = false;
        return;
    }
    var req = this.queue.shift();
    this.request(req.method, req.url, req.callback, req.postVars, true);
};


/* OfflineHandler class. */
var OfflineHandler = function() {
    this.storedRequests = [];
};

extend(OfflineHandler, SimpleHandler);

OfflineHandler.prototype.request = function(method, url, callback, postVars) {
    if (XhrManager.isOffline) { //Store the request until we are online.
        this.storeRequest.push({
            method: method,
            url: url,
            callback: callback,
            postVars: postVars
        });
    }else{// Call SimpleHandler's request method if we are online.
        this.flushStoredRequests();
        OfflineHandler.superclass.request(method, url, callback, postVars);
    }
};

OfflineHandler.prototype.flushStoredRequests = function() {
    for(var i = 0, len = storedRequests.length; i < len; i++) {
        var req = storedRequests[i];
        OfflineHandler.superclass.request(req.method, req.url, req.callback, req.postVars);
    }
};

/* XhrManager singleton. */

var XhrManager = {
    createXhrHandler: function() {
        var xhr;
        if (this.isOffline()) {
            xhr = new OfflineHandler();
        }
        else if (this.isHighLatency) {
            xhr = new QueuedHandler();
        }
        else{
            xhr = new SimpleHandler();
        }
       
        Interface.ensureImplements(xhr, AjaxHandler);
        return xhr;
    },
   
    isOffline: function() { // Do a quick request with SimpleHandler and see if it succeeds.
        //...
    },
   
    ifHighLatency: function() { // Do aseries of requests with SimpleHandler and time the response. Best done once, as a branching funciton.
        //...
    }
   
};

//var myHandler = XhrManager.createXhrHandler();
//var callback = {
//    success: function(responseText) { alert('Successs: '+ responseText); },
//    failure: function(statusCode) { alert('Failure: '+ statusCode); }
//};

//myHandler.request('GET', 'sctipt.php', callback);

 

如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答??! 點(diǎn)擊進(jìn)入論壇

發(fā)表評(píng)論 (273人查看,0條評(píng)論)
請(qǐng)自覺(jué)遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)