ExtJS: How do I get raw JSON data from Ext.data.Store?
Asked Answered
G

7

13

From a typical store like this

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'firstName', type: 'string'},
        {name: 'lastName',  type: 'string'},
        {name: 'age',       type: 'int'},
        {name: 'eyeColor',  type: 'string'}
    ]
});

var myStore = Ext.create('Ext.data.Store', {
    model: 'User',
    proxy: {
        type: 'ajax',
        url : '/users.json',
        reader: {
            type: 'json',
            root: 'users'
        }
    },
    autoLoad: true
});

Is it possible to get raw Json from myStore?

Gunning answered 5/2, 2012 at 17:37 Comment(0)
J
17

What about:

myStore.proxy.reader.rawData
Juieta answered 16/2, 2012 at 9:50 Comment(2)
@Tenerezza So what's the difference? Of course we are able to access the data only after the data is loaded into the store...Juieta
The difference is that you need to go by store.getProxy().getReader().rawData to access the data.Shuster
D
12

The accepted solution did not work in my configuration: ExtJs 4.1 and a grid with a memory proxy - it returned empty after I added items to the gird (and the store object reflected that). The following code worked for encoding store contents into a JSON string:

var json = Ext.encode(Ext.pluck(store.data.items, 'data'));

Cheers!

Droopy answered 17/10, 2013 at 15:9 Comment(2)
While this solution works, it relies on the private data property and as such is not guaranteed to work in future releases of ExtJs. It's unfortunate that ExtJs does not have a supported way of doing this.Hero
6.5.x: Ext.encode(Ext.Array.pluck(store.data.items, 'data'));Procarp
S
4

Took me ages to find a solution to this but here is one solution that will work.

    myStore .on({ 'load': function (store, records, successful)
    {
        console.log(store.getProxy().getReader().rawData);
    } 
    });
Shuster answered 6/7, 2012 at 12:8 Comment(0)
W
1

The proxy reader rawData/jsonData will become accessible after the store is created. To access it try the following:

store.on('load', function() {
  console.log('store.proxy.reader.jsonData');
  console.log(store.proxy.reader.jsonData);
}, this, { single: true });
Whitener answered 1/3, 2013 at 1:22 Comment(0)
P
1
Ext.data.Store({....
            proxy: {
                       ...
                       reader: {
                       **keepRawData: true,**
                       type: 'json',
                       rootProperty: 'root',
                       successProperty: 'success'
                       }
                    }
                }); 
Petroleum answered 11/11, 2016 at 12:8 Comment(0)
G
-1

Look like I found the solution by hooking event listeners to data store:

var myStore = Ext.create('Ext.data.Store', {
  ...
  listeners: {
    'load': {
      fn: function(store, records, success, operations) {
        Ext.each(records, function(rec) {
          console.log(Ext.encode(rec.raw));
        });
      }
    }
  }
  ...
});
Gunning answered 6/2, 2012 at 2:41 Comment(1)
This only get the data from the root json node but not the rest of it.Shuster
P
-1

maybe, you can try with this:

var json = Ext.JSON.encode(myStore.getRange());

cya!

Palpitant answered 10/4, 2013 at 15:48 Comment(1)
That'll give you a "maximum call stack size exceeded" error.Notation

© 2022 - 2024 — McMap. All rights reserved.