【问题标题】:Extjs Restful Store, Sending request in Batch?Extjs Restful Store,批量发送请求?
【发布时间】:2011-05-22 03:56:23
【问题描述】:

我创建了一个 Grid 组件,其存储配置如下:

    //Create the store
    config.store = new Ext.data.Store({
        restful: true,
        autoSave: false,
        batch: true,
        writer: new Ext.data.JsonWriter({
            encode: false
        }),
        reader: new Ext.data.JsonReader({
            totalProperty: 'total',
            root: 'data',
            fields: cfg.fields
        }),
        proxy: new Ext.data.HttpProxy({
            url:cfg.rest,
            listeners:{
                exception: {
                    fn: function(proxy, type, action, options, response, arg) {
                        this.fireEvent('exception', proxy, type, action, options, response, arg);
                    },
                    scope: this
                }
            }
        }),
        remoteSort: true,
        successProperty: 'success',
        baseParams: {
            start: 0,
            limit: cfg.pageSize || 15
        },
        autoLoad: true,
        listeners: {
            load: {
                fn: function() {
                    this.el.unmask();
                },
                scope: this
            },

            beforeload: {
                fn: function() {
                    this.el.mask("Working");
                },
                scope: this
            },
            save: {
                fn: function(store, batch, data) {
                    this.el.unmask();
                    this.fireEvent('save', store, batch, data);
                },
                scope: this
            },

            beforewrite: {
                fn: function(){
                    this.el.mask("Working...");
                },
                scope: this
            }

        }
    });

注意:忽略 fireEvents。此存储正在共享自定义网格组件中进行配置。

但是,我在这里有一个问题:无论我做了什么 CRUD 操作,我总是向服务器发出 N 个请求,这等于我选择的 N 行。即,如果我选择 10 行并点击 Delete,则会向服务器发出 10 个 DELETE 请求。

例如,我是这样删除记录的:

/**
 * Call this to delete selected items. No confirmation needed
 */
_deleteSelectedItems: function() {
    var selections = this.getSelectionModel().getSelections();
    if (selections.length > 0) {
        this.store.remove(selections);
    }
    this.store.save();
    this.store.reload();
},

注意:“this”的范围是一个网格组件。

那么,它应该是这样的吗?还是我的配置问题? 我用的是Extjs 3.3.1,根据Ext.data.Store下batch的文档,

如果 Store 是 RESTful,DataProxy 也是 RESTful,并且为每条记录生成一个唯一的事务。

我希望这是我的配置问题。

注意:我尝试在 Ext.data.JsonWriter 中使用 listfulencodewriteAllFieldsencodeDelete... 没有希望

【问题讨论】:

    标签: javascript rest extjs store extjs3


    【解决方案1】:

    仅供那些可能想知道为什么它不是批处理的人:

    至于说明的文件,

    如果 Store 是 RESTful,DataProxy 也是 RESTful,并且为每条记录生成一个唯一的事务。

    如果你在/src/data/Store.js 中查看Ext.data.Store 的源代码,这是正确的

    第 309 行,@constructor

    // If Store is RESTful, so too is the DataProxy
    if (this.restful === true && this.proxy) {
        // When operating RESTfully, a unique transaction is generated for each record.
        // TODO might want to allow implemention of faux REST where batch is possible using RESTful routes only.
        this.batch = false;
        Ext.data.Api.restify(this.proxy);
    }
    

    这就是为什么我意识到当我使用restful 时,我的batch 永远不会更改为true

    【讨论】:

    • 优秀的答案!从我的观点来看,完全令人讨厌,但这不是你的错。不过,我有点好奇为什么 Sencha 认为 rest 每个 RESTful 事务需要一个 xmlhttprequest。批处理似乎并没有改变状态匹配的前提。
    • 哇,谢谢。但这有点旧(2010)!这仍然适用于现有的 v4.1 吗?哈哈!
    【解决方案2】:

    您正确阅读了文档;它应该以这种方式工作。 It's something to consider whenever choosing whether to use RESTful stores on your grids.如果您需要批量操作,RESTful 存储不是您的朋友。对不起。

    【讨论】:

    • 嘿 wes,非常感谢您的回答。我认为这是我犯的导致此问题的配置错误之一,至少您已与我确认。
    • @justinzane - 我不知道 ExtJS 4。这个问题是关于 ExtJS 3 的。关于 v4 restful 批处理请求可能值得自己提出问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    相关资源
    最近更新 更多