Extjs Restful Store, Sending request in Batch?

2019-07-02 20:49发布

I created a Grid component with the store configuration like this:

    //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
            }

        }
    });

Note: Ignore the fireEvents. This store is being configured in a shared custom Grid Component.

However, I have one problem here: Whatever CRUD actions I did, I always come out with N requests to the server which is equal to N rows I selected. i.e., if I select 10 rows and hit Delete, 10 DELETE requests will be made to the server.

For example, this is how I delete records:

/**
 * 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();
},

Note: The scope of "this" is a Grid Component.

So, is it suppose to be like that? Or my configuration problem? I'm using Extjs 3.3.1, and according to the documentation of batch under Ext.data.Store,

If Store is RESTful, the DataProxy is also RESTful, and a unique transaction is generated for each record.

I wish this is my configuration problem.

Note: I tried with listful, encode, writeAllFields, encodeDelete in Ext.data.JsonWriter... with no hope

2条回答
混吃等死
2楼-- · 2019-07-02 21:26

You read the docs correctly; it is supposed to work that way. It's something to consider whenever choosing whether to use RESTful stores on your grids. If you're going to need batch operations, RESTful stores are not your friends. Sorry.

查看更多
SAY GOODBYE
3楼-- · 2019-07-02 21:38

Just for those who might wonder why it's not batch:

As for the documentation stated,

If Store is RESTful, the DataProxy is also RESTful, and a unique transaction is generated for each record.

Which is true if you look into the source code of Ext.data.Store in /src/data/Store.js

Line 309, in @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);
}

And so this is why I realize when I use restful, my batch will never get changed to true.

查看更多
登录 后发表回答