1

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

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

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

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

我想出的解决方案如下。

4

1 回答 1

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 );
    }
});
于 2013-05-31T10:28:22.507 回答