1. #1
    Ext JS Premium Member antonye's Avatar
    Join Date
    May 2010
    Location
    London, UK
    Posts
    27
    Vote Rating
    0
    antonye is on a distinguished road

      0  

    Question Configuration of JsonReader - root: as the "real root" of data?

    Configuration of JsonReader - root: as the "real root" of data?


    Quick question - I think I know the answer to this one, but just want to check with someone who has more experience with ExtJS and JSON than I do!

    I have a JsonReader, which is working fine, but I want to expand the scope of the data that it reads; the reason being that my data returned looks something like this:

    Code:
    {
        errors: null,
        success: true,
        a: {
            somedata: blah,
            moredata: blah
        },
        b: {
            anothernode: blah,
            yetmoredata: blah
        },
        c: {
            etcetc: blah,
            etcetcetc: blah
        }
    }
    So when I bind my JsonReader as "root: a" this works fine, and I can read self.getAt(0).get('somedata') without any problems at all.

    But I want to expand the scope of my reader so that I can also read the nodes "b" and "c" as well.

    Obviously what I want to be able to do is to bind the "root:" to ... well, the root of the JSON data returned, but I'm scratching my head because (a) the API says I have to bind it to *something* and (b) it doesn't like "root: '' " or combinations of empty/null to that effect!

    So is it possible to bind the JsonReader.root to the root of my JSON data, or do I need to encapsulate my current JSON data model within another level so that I can then bind it to this new root, eg:

    Code:
    {
        errors: null,
        success: true,
        newroot: {
            a: {
                somedata: blah,
                moredata: blah
            },
            b: {
                anothernode: blah,
                yetmoredata: blah
            },
            c: {
                etcetc: blah,
                etcetcetc: blah
            }
        }
    }
    Like I said, I think I know the answer to this one, so I guess I'm looking for confirmation

  2. #2
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    No need to modify your data. You only have to specify a special function for the root that extracts the data as an array, e.g.
    Code:
    root: function(data){
        var result = [];
        for(var prop in data){
            if(data.hasOwnProperty(prop) && Ext.isObject(data[prop]){
                result.push(Ext.apply({id: prop}, data[prop]);
            }
        }
        return result;
    }
    (only works on Ext 3.2+)

  3. #3
    Ext JS Premium Member antonye's Avatar
    Join Date
    May 2010
    Location
    London, UK
    Posts
    27
    Vote Rating
    0
    antonye is on a distinguished road

      0  

    Default


    Awesome, thankyou!

    I was wondering if there would be some way of implementing a customised root (I had read about swapping the root in a pre-forum-post search!) but my inexperience in ExtJS was holding me back.

Similar Threads

  1. [Solved]Ext.data.JsonStore giving a "root is undefined" error.
    By nickweavers in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 26 Dec 2009, 11:05 AM
  2. Replies: 2
    Last Post: 29 Nov 2009, 8:47 AM
  3. datastore load error"root is undefined"
    By netbox in forum Ext 2.x: Help & Discussion
    Replies: 5
    Last Post: 17 Mar 2009, 1:39 AM
  4. Why the tree only display "Tree Root", no children???
    By deardeer76 in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 13 Feb 2009, 7:49 PM
  5. Replies: 9
    Last Post: 20 Jun 2007, 7:36 PM

Thread Participants: 1