How to configure ExtJS 4 Store (proxy and reader) to read metadata

My question is how to get metadata other than totalRecords, in my case it is version, code, searchquery (look at json).

{ "result": { "version":"1", "code":"200", "searchquery": "false", "totalRecords": "2", "account":[ { "lastname": "Ivanoff", "firstname": "Ivan", "accountId":"1" }, { "lastname": "Smirnoff", "firstname": "Ivan", "accountId":"2" } ] } 

}

Here is my model:

 Ext.define("test.Account", { extend: "Ext.data.Model", fields: [ {name: 'accountId', type: 'string'}, {name: 'lastname', type: 'string'}, {name: 'firstname', type: 'string'} ] }); 

And save:

 Ext.define("test.TestStore", { extend: "Ext.data.Store", model: "test.Account", proxy: { type: "ajax", url: "users.json", reader: { type : 'json', root : 'result.account', totalProperty: "result.totalRecords" } }, listeners: { load: function(store, records, success) { console.log("Load: success " + success); } } }); 

Using this store, I can download records (account) and cannot find any methods for accessing the rest of the fields.

Thanks in advance.

+8
json proxy extjs extjs4 model
source share
3 answers

Here is the solution for my problem. I am handling the afterRequest event in the Proxy class, where I can get the response data, parse it and save the metadata. This is the proxy part of the TestStore class:

So, here is the proxy part from the TestStore class:

 proxy: { type: "ajax", url: "/users.json", reader: { type : 'json', root : 'gip.account', totalProperty: "gip.totalRecords", searchquery: "searchquery" }, afterRequest: function(req, res) { console.log("Ahoy!", req.operation.response); } } 
+17
source share

You can use the metachange event in the repository.

All specific information not related to extjs can be grouped in JSON in a separate object:

 { "result": { "totalRecords": "2", "account":[ { "lastname": "Ivanoff", "firstname": "Ivan", "accountId":"1" }, { "lastname": "Smirnoff", "firstname": "Ivan", "accountId":"2" } ] }, "myMetaData": { "version":"1", "code":"200", "searchquery": "false" } } 

Store configured as

 Ext.define("test.TestStore", { extend: "Ext.data.Store", model: "test.Account", proxy: { type: "ajax", url: "users.json", reader: { type : 'json', root : 'result.account', totalProperty: "result.totalRecords", metaProperty: 'myMetaData' } }, listeners: { metachange: function(store, meta) { console.log("Version " + meta.version + "Search query " + meta.searchQuery); } } }); 
+3
source share

Take a look at the Ext.data.Proxy class and more specifically processResponse() . If you need to extract any additional data, you will have to extend the standard class and change this method.

+1
source share

All Articles