2013-05-31 91 views
1

在我正在開發的日曆系統中,從服務器中選擇日期提取該日期的事件。確保僅處理對最新(直接)代理讀取請求的響應

問題是,如果用戶在日期之間快速切換,有時第二個請求的響應會在對第一個請求的響應之前返回,從而導致存儲由錯誤記錄填充(第一個請求的記錄,即錯誤的日期)。

我嘗試過使用Ext.Ajax.abort(),但這似乎不適用於direct調用。

所以問題是如何確保只有最新的請求響應由代理處理?

我想出的解決方案如下。

回答

1

有可能從直接代理類派生,以確保這樣的行爲:

/** 
* A proxy class that ensures only the reponse to the last read request is 
* processed. 
* 
* A quick user actions may result in more than one request sent to the server, 
* but it is possible for the server to return a response to the second request 
* before returning that of the first request. This will mean the the store 
* will be populated with records that do not correspond to the latest user 
* action. 
* 
*/ 

Ext.define('Ext.data.proxy.SerialDirect', { 

    extend: 'Ext.data.proxy.Direct', 
    alternateClassName: 'Ext.data.DirectSerialProxy', 

    alias: 'proxy.serialdirect', 

    doRequest: function(operation, callback, scope) { 
     this.callParent(arguments); 

     // Store the last read request 
     if (operation.request.action == "read") { 
      this.lastReadRequest = operation.request; 
     } 
    }, 

    processResponse: function(success, operation, request, response, callback, scope) {    
     // abort if the request is a read one and does not correspond to the 
     // last read request 
     if (request.action == "read" && request != this.lastReadRequest) 
      return; 

     this.callParent(arguments); 
    } 
}); 
+0

感謝分享+1 – sra