Results 1 to 1 of 1

Thread: How to: get a json enabled store to return nested data (possible solution!)

  1. #1
    Sencha User
    Join Date
    Jan 2011
    Vote Rating

    Lightbulb How to: get a json enabled store to return nested data (possible solution!)


    for a project of mine, a json enabled store is used to communicate with a restfull api (cakePHP).
    The json it receives when loading data is the following (very simple):


    As u can see it is nested data, with User as a key.
    This is default cakePHP behavior, and I haven't been able to change this in an easy way.

    I read the documentation regarding json readers and it states you can provide a "record" name where the record data itself can be found at. But when I set "User" as the record, nothing happens.
    I couldnt figure out why this isnt working so I created a "fix". See the code below:

    I've read a lot of posts on this subject but couldn't find the solution, so I've come up with the following. I hope somebody here can give me feedback on this, because I'm new at sencha and maybe the change has undesired sideeffects. I haven't discovered any so far though.

    Basically, what I do is override the readRecords function of the jsonreader to loop through the fetched and parsed records, building a new array of "User" objects in the process. As you can see it uses the record variable of the jsonreader to verify which object it should fetch.

    Ext.override(, {
       readRecords: function(data) {
            //this has to be before the call to super because we use the meta data in the superclass readRecords
            if (data.metaData) {
             * DEPRECATED - will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead
             * @property jsonData
             * @type Mixed
            this.jsonData = data;
            // Loop through data, using "record" to create a a new data object
            var newData = new Array();
            var record = this.record;
            Ext.each(data, function(d,index){        
             newData[index] = (eval('d.'+record));
    app.stores.users = new{
        model: 'app.models.User',
        proxy: {
            type: 'rest',
            url : '/api/users.json',
            reader: {
                type : 'json',
                root: '',
                record: 'User'
    I hope this helps someone else. It's pretty simple. I can imagine one would want to get other nested data, but this was out of my scope.

    If there's a better solution, please let me know. Feel free to copy/edit the code above.

    Kind regards,

    Last edited by jochem kuun; 5 Apr 2011 at 12:13 AM. Reason: minor correction

Similar Threads

  1. return json data where 'root' is not an array
    By Frances in forum Sencha Touch 1.x: Discussion
    Replies: 13
    Last Post: 8 Jul 2011, 7:43 AM
  2. How do you handle Java JSON return data?
    By Litre in forum Community Discussion
    Replies: 5
    Last Post: 25 Jan 2010, 8:09 AM
  3. Ext 3.1 EditorGridPanel and JSON/REST where remote CUD does not return data
    By jmaasing in forum Ext 3.x: Help & Discussion
    Replies: 1
    Last Post: 19 Jan 2010, 6:05 AM
  4. How to return data from the server that is not XML or JSON
    By rtikku in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 28 Nov 2008, 4:06 AM
  5. Wait until Json has return data
    By Jacsoft in forum Ext 2.x: Help & Discussion
    Replies: 7
    Last Post: 16 Jun 2008, 11:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts