3

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lionel Chan
  • 7,894
  • 5
  • 40
  • 69

2 Answers2

6

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.

Lionel Chan
  • 7,894
  • 5
  • 40
  • 69
  • 1
    Excellent Answer! Completely fscking annoying, from my POV, but that ain't your fault. I'm kinda curious why Sencha thinks that rest requires one xmlhttprequest per RESTful transaction, though. Batching does not seem to alter the premise of state matching. – justinzane Nov 17 '12 at 22:55
  • Wow thanks. But this is kinda old (2010)! Is this still applicable to the existing v4.1? Lolx! – Lionel Chan Nov 19 '12 at 02:30
2

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.

wes
  • 7,795
  • 6
  • 31
  • 41
  • Hey wes, thanks a lot for your answer. I thought this is one of the configuration mistake I made which raises this issue, at least you have confirmed it with me. – Lionel Chan Dec 09 '10 at 02:23
  • @justinzane - I don't know about ExtJS 4. This question is about ExtJS 3 specifically. It's probably worth its own question to ask about v4 restful batch requests. – wes Nov 17 '12 at 18:02