c.sokun
c.sokun

Reputation: 1662

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

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?

Upvotes: 13

Views: 38219

Answers (7)

Y.Coding
Y.Coding

Reputation: 109

Ext.data.Store({....
            proxy: {
                       ...
                       reader: {
                       **keepRawData: true,**
                       type: 'json',
                       rootProperty: 'root',
                       successProperty: 'success'
                       }
                    }
                }); 

Upvotes: 1

Timur K
Timur K

Reputation: 426

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!

Upvotes: 12

mfruizs
mfruizs

Reputation: 770

maybe, you can try with this:

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

cya!

Upvotes: -1

user867768
user867768

Reputation:

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

Upvotes: 1

Tenerezza
Tenerezza

Reputation: 394

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

Upvotes: 4

Grant Zhu
Grant Zhu

Reputation: 3018

What about:

myStore.proxy.reader.rawData

Upvotes: 17

c.sokun
c.sokun

Reputation: 1662

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

Upvotes: -1

Related Questions