json 如何配置 ExtJS 4 Store(代理和阅读器)来读取元数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11000819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:23:13  来源:igfitidea点击:

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

jsonextjsmodelproxyextjs4

提问by barmaleikin

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

我的问题是如何获取除 totalRecords 之外的元数据,在我的情况下是版本、代码、搜索查询(请查看 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.

先感谢您。

回答by barmaleikin

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:

这是我的问题的解决方案。我正在 Proxy 类中处理 afterRequest 事件,我可以在其中获取响应数据、解析它并保存元数据。这是 TestStore 类的代理部分:

So here is proxy part from the TestStore class:

所以这里是 TestStore 类的代理部分:

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);    
        }
    }

回答by Ivan Prasol

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

可以使用商店的“metachange”事件。

All the non-extjs specific information can be grouped in JSON in the separate object:

所有非 extjs 特定信息都可以在 JSON 中分组到单独的对象中:

{
    "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);     
        }
    }
});

回答by sha

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

看看Ext.data.Proxy类,更具体地说是processResponse()方法。如果您需要提取任何其他数据 - 您将不得不扩展标准类并更改该方法。