javascript - How to create more than one JSON store from a get request? - Stack Overflow

Lets suppose I have this web application built using extjs4 in my client-side and a Zend framework acti

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?!

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?!

Share Improve this question edited Sep 4, 2011 at 8:44 Mehdi Fanai asked Sep 3, 2011 at 15:52 Mehdi FanaiMehdi Fanai 4,06913 gold badges53 silver badges76 bronze badges 7
  • Are you asking how to format the JSON to represent multiple stores? – jfriend00 Commented Sep 3, 2011 at 15:57
  • @jfriend00 actualy im asking how to process that JSON on client side to create multiple stores from that JSON. – Mehdi Fanai Commented Sep 3, 2011 at 17:33
  • In your JSON response, what pieces of data constitute a "store"? We don't know what that term means. I see one set of admin settings, and then a list of countries and a list of languages. Which of those pieces of data are a "store"? – jfriend00 Commented Sep 3, 2011 at 17:38
  • 1 He is referring to "store" in the Ext.data.Store sense. He wants to use ONE get request, and populate 2 (or more) Ext.data.Store's. I want the answer to this too. :) – LittleTreeX Commented Sep 3, 2011 at 20:35
  • You will need to define the fields; that is simply the way it works. You can actually send over more data than what gets put into the store (and there are probably use cases for this). Also, you should generally define your fields in a Model, and then point your store to said Model (especially if you are going to implement the SMVC paradigm). – LittleTreeX Commented Sep 3, 2011 at 20:39
 |  Show 2 more ments

2 Answers 2

Reset to default 5

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

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745602235a4635468.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信