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

╄→гoц情女王★ 提交于 2020-01-22 05:55:06

问题


My question is how to get metadata besides totalRecords, in my case it is version, code, searchquery (please 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 store:

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 am able to load records (account) and cannot find any methods to access rest of the fields.

Thank you in advance.


回答1:


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

So here is 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);    
        }
    }



回答2:


It is possible to use the 'metachange' event of the store.

All the non-extjs specific information can be grouped in JSON in the 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"
    }
}

The store is 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:


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



来源:https://stackoverflow.com/questions/11000819/how-to-configure-extjs-4-store-proxy-and-reader-to-read-metadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!