2011-02-04 78 views
1

我必須爲多個打開/關閉模式請求創建一個併發的函數。併發調用一個簡單對象

例如:當我調用showAjaxLoading(true)時,它顯示模態,showAjaxLoading(false)它處置模態。

問題:當我發出第一個長顯示模態的請求時,另一個快速請求會關閉它。我希望能夠將所有請求保留在數組中,並且只有在最後一個請求結束時才能處理模式。

SimpleModal: simplemodal對象是唯一的。當你創建模態時,它會返回對象本身。但是當你已經打開一個模式時,它會返回false。 url:http://www.ericmmartin.com/projects/simplemodal/

var showAjaxLoading = function (show) { 
    var ajaxModal; 
    if (show) { 
     var aModal = $("#ajaxLoading").modal({ overlayId: 'ajaxloading-overlay', containerId: 'ajaxloading-container', closeClass: 'ajaxloading-close', close: false, escClose: false }); 
     if (aModal !== false) { 
      ajaxModal = aModal; 
     }; 
    } else { 
     if (ajaxModal !== undefined && $.isFunction(ajaxModal.close)) { 
      ajaxModal.close(); 
     }; 
    }; 
}; 

什麼是解決此問題的最佳解決方案?

回答

2
var modalDialog = { 
    _requestsInProcess: 0, 

    showAjaxLoading : function() 
    { 
     if (this._requestsInProcess == 0) 
     { 
      // open overlay here 
     } 

     this._requestsInProcess++; 
    }, 

    hideAjaxLoading : function() 
    { 
     this._requestsInProcess--; 
     if (this._requestsInProcess == 0) 
     { 
      // hide overlay here 
     } 
    } 
} 

嘗試是這樣的/現在你可以調用modalDialog.showAjaxLoading(),您將AJAX請求和modalDialog.hideAjaxLoading()每次你的要求完成各一次。

相關問題