如何根据 get 请求创建多个 JSON 存储?

发布于 2024-12-02 16:40:54 字数 1060 浏览 0 评论 0原文

假设我在客户端使用 extjs4 构建了这个 Web 应用程序,在服务器端使用 Zend 框架操作控制器。我在服务器端有一组数组,它​​们可以作为 JSON 输出到客户端。当我只想创建一个存储时,通常的方式是这样工作的:

    Ext.define('MA.store.AdminResources', {
extend : 'Ext.data.Store',
fields : [ {
    name : 'id'
}, {
    name : 'name'
} ],
autoLoad : true,
proxy : {
    type : 'ajax',
    url : './account/adminresources',
    reader : {
        type : 'json'
        //,root : 'users'
    }
}

});

如果我想从单个 http 创建多个 JSON 存储怎么办向服务器请求? 该服务器不会接收每个商店的远程代理请求,并且将为所有商店完成一个请求。 以下是服务器返回的 JSON 的示例:

{
"adminsettings"{"userid":3333,"primaryemail":"[email protected]","firstname":"","middlename":null}
,"countries":{"AD":"Andorra","AE":"UnitedArabEmirates","AF":"Afghanistan"...}
,"languages":{"aa":"Afar","ab":"Abkhazian","ace":"Achinese","ach":"Acoli"...
}}

How can JSON stores for adminsettings,countries, languages be create with just one http request to the server? 也许我需要定义 1 个代理和 3 个读取器?!

Lets suppose I have this web application built using extjs4 in my client-side and a Zend framework action controller in server-side. I have array of arrays in server-side that they could be output as JSON to the client.when i want to create only one store,usual way is working like this:

    Ext.define('MA.store.AdminResources', {
extend : 'Ext.data.Store',
fields : [ {
    name : 'id'
}, {
    name : 'name'
} ],
autoLoad : true,
proxy : {
    type : 'ajax',
    url : './account/adminresources',
    reader : {
        type : 'json'
        //,root : 'users'
    }
}

});

what if i wanted to create multiple JSON stores from just A single http request to the server?
this server will not receive a remote proxy request per store and one request will be done for ALL stores.
Here is an example of my JSON which is returned by server:

{
"adminsettings"{"userid":3333,"primaryemail":"[email protected]","firstname":"","middlename":null}
,"countries":{"AD":"Andorra","AE":"UnitedArabEmirates","AF":"Afghanistan"...}
,"languages":{"aa":"Afar","ab":"Abkhazian","ace":"Achinese","ach":"Acoli"...
}}

How can JSON stores for adminsettings,countries,languages be created with just one http request to the server?
perhaps i need to define one proxy and 3 readers?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2024-12-09 16:40:54

正如您在文档中看到的,您可以定义不带代理的存储:

Ext.create('Ext.data.Store', {
    model: 'User',
    data : [
        {firstName: 'Ed',    lastName: 'Spencer'},
        {firstName: 'Tommy', lastName: 'Maintz'},
        {firstName: 'Aaron', lastName: 'Conran'},
        {firstName: 'Jamie', lastName: 'Avins'}
    ]
});

然后很容易发出 ajax 请求和 onSuccess 手动加载数据:

如下所示:

adminSettings = Ext.create('Ext.data.Store', {model: AdminSettings});
countries = Ext.create('Ext.data.Store', {model: Country});

Ext.ajax.request({
    url: './account/adminresources',
    success: function(response) {
        var json = Ext.decode(response.responseText);
        adminsettings.loadData(json.adminsettings);
        countries.loadData(json.countries);
        //...
    } 
});

As you can see in the documentation you can define store without proxy:

Ext.create('Ext.data.Store', {
    model: 'User',
    data : [
        {firstName: 'Ed',    lastName: 'Spencer'},
        {firstName: 'Tommy', lastName: 'Maintz'},
        {firstName: 'Aaron', lastName: 'Conran'},
        {firstName: 'Jamie', lastName: 'Avins'}
    ]
});

then It's easy to make an ajax request and onSuccess to load data manualy:

Something like this:

adminSettings = Ext.create('Ext.data.Store', {model: AdminSettings});
countries = Ext.create('Ext.data.Store', {model: Country});

Ext.ajax.request({
    url: './account/adminresources',
    success: function(response) {
        var json = Ext.decode(response.responseText);
        adminsettings.loadData(json.adminsettings);
        countries.loadData(json.countries);
        //...
    } 
});
喜你已久 2024-12-09 16:40:54

在阅读 extjs 学习中心网格常见问题解答时,我找到了确切的问题和我的问题的答案。以下是问题和答案:

使用一个 AJAX 请求加载多个商店?

还有一个使用 XML 的示例 此处

将数据从作为单个 http 请求的一部分返回的单个 json 字符串显示到多个数据存储。

选项 1:查看此帖子

//create a JSON object:
{
dataStore1: /*1st json string */,
dataStore2: /*2nd json string */
}
//decode the json packet...
var json = Ext.decode(response.responseText);
//load the stores:
store1.loadData(json.dataStore1);
store2.loadData(json.dataStore2);

选项 2:

 //create a JSON object:
 {
 dataStore1: /*1st json string */,
 dataStore2: /*2nd json string */
 }
 //decode the json packet...
 var json = Ext.decode(response.responseText);
 //create new proxy data for stores as hendricd mentioned:
 store1.proxy.data = json.dataStore1;
 store2.proxy.data = json.dataStore2;
 //load stores
 store1.load();
 store2.load();

 //where stores' proxy has to be defined like this:
 proxy: new Ext.ux.data.BufferedPagingMemoryProxy([])
 //I guess this can be used in case someone is using PMP (paging memory proxy)

While reading on extjs learning center grid faq i found the exact question and answer to my question. Here is the question and the answer:

Load multiple stores with one AJAX request?

There is also an example using XML here

display data to multiple data stores from a single json string that a returned as part of a single http request.

Option 1: (see this thread)

//create a JSON object:
{
dataStore1: /*1st json string */,
dataStore2: /*2nd json string */
}
//decode the json packet...
var json = Ext.decode(response.responseText);
//load the stores:
store1.loadData(json.dataStore1);
store2.loadData(json.dataStore2);

Option 2:

 //create a JSON object:
 {
 dataStore1: /*1st json string */,
 dataStore2: /*2nd json string */
 }
 //decode the json packet...
 var json = Ext.decode(response.responseText);
 //create new proxy data for stores as hendricd mentioned:
 store1.proxy.data = json.dataStore1;
 store2.proxy.data = json.dataStore2;
 //load stores
 store1.load();
 store2.load();

 //where stores' proxy has to be defined like this:
 proxy: new Ext.ux.data.BufferedPagingMemoryProxy([])
 //I guess this can be used in case someone is using PMP (paging memory proxy)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文