确保仅处理对最新(直接)代理读取请求的响应

时间:2013-05-31 10:28:23

标签: extjs extjs4.1

在我正在开发的日历系统中,从服务器中选择该日期的日期获取事件。

问题在于,如果用户在日期之间快速切换,有时会在对第一个请求的响应之前返回第二个请求的响应,从而导致存储由错误的记录(第一个请求的记录)填充,即错误的日期)。

我尝试使用Ext.Ajax.abort(),但这似乎不适用于direct次来电。

所以问题是如何确保代理只处理最新请求的响应?

我想出的解决方案如下。

1 个答案:

答案 0 :(得分: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 );
    }
});