AjaxManager的實現

來源:互聯網
上載者:User

標籤:

在NeralJS思路整理一章中我們提到過實用ajax管理模組控制ajax請求,以減少ajax請求數量,最佳化載入,以下是ajax模組的實現,我已經確保經曆簡單。

/** * Created by william on 2014/12/26. */define(function () {    var events = require("EventBus");    function AjaxManager(serverURL) {        if (!serverURL || serverURL == "" || serverURL.length == 0)            throw "web服務地址未定義";        this.serverURL = serverURL;        this.request_list = [];        this.data_list = [];    }    /**     * 添加請求到待發區     * @param requestInfo 資料格式{uid: ‘kb123‘,dataRange: { start: ‘2014/11/8‘, end: ‘2014/-1/-1‘ }}     */    AjaxManager.prototype.addRequest = function (requestInfo) {        this.request_list.push(requestInfo);    };    /**     * 發出儲存在請求列表中的請求,請求完成時觸發request_ready事件,注意data資料是一組key:value對     * @constructor     */    AjaxManager.prototype.DoRequest = function () {        var self = this;        $.post(this.serverURL, this.request_list, function (data) {            for (var k in data) {                self.data_list[k] = data[k];            }            events.trigger(self, "request_ready", self.data_list);        });        this.request_list = [];//請求完成,清空請求隊列    };    /**     * 訂閱Ajax完成事件,該事件在ajax返回時被觸發     * @param callback     * @constructor     */    AjaxManager.prototype.OnReady = function(callback){        events.addListener(this,"request_ready",callback);    };});

上面這個模組無法實現資料的定向推送,說以將整個this.data_list都開發給了每一個訂閱者。這也是收到EventBus模組功能的局限性影響,不過對於網頁來說,所有返回的資料本來就是要顯示出來的,所以不存在安全性問題。暫時足夠使用

AjaxManager的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.