How to load json store with multiple root elements?

In one of my projects, I need to download the Json store with the JSOn server response as follows. In JSon's answer, I get 2-3 root elements. but in the store configuration I can provide only one root element.

{
    {"level2List":[{id:'id1', name:'sample'},....]},
    {"level3List":[{id:'id1', name:'sample'},....]},
    {"level4List":[{id:'id1', name:'sample'},....]}
}

My store configuration as below.

store = new Ext.data.JsonStore({
// store configs
storeId: 'myStore',
proxy: {
    type: 'ajax',
    url: 'xml/getKpiInputData.json',
    reader: {
        type: 'json',
        root: 'level3List',
        idProperty: 'name'
    }
},
fields: [
        {name: 'name'},
        {name: 'id'},
       ...
    ],
remoteFilter: false,
remoteSort: true,
    pageSize: 10,
autoLoad: {start: 0, limit: 10}
 });

If I give 1 root element (e.g. level3List), it loads the corresponding elements correctly. But I need a solution to load data from multiple root elements. Please help me in uploading data to the store.

+5
source share
1 answer

If you are using 4.x, the root parameter may be a function:

Ext.define('MyModel', {
    extend: 'Ext.data.Model',
    fields: ['name']
});

Ext.require('*');

Ext.onReady(function(){

    var store = new Ext.data.Store({
        model: MyModel,
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: function(o){
                    var out = [];
                    return out.concat(o.root1, o.root2, o.root3);
                }
            }
        },
        data: {
            root1: [{
                name: 'Item 1'
            }],
            root2: [{
                name: 'Item 2'
            }],
            root3: [{
                name: 'Item 3'
            }]
        }
    });

    store.load();
    console.log(store.getCount());
});
+5
source

All Articles