Results 1 to 7 of 7

Thread: Json Data, with nested array

  1. #1
    Sencha User
    Join Date
    Jun 2009
    Location
    Burlington, Ontario
    Posts
    175

    Default Json Data, with nested array

    I have a json string as follows;

    PHP Code:
    {
        
    "data": {
            
    "categories": [
                
    "M",
                
    "A" 
            
    ],
            
    "series": [
                
    "Projects" 
            
    ],
            
    "seriesdata": [
                
    13,
                
    20 
            

        },
        
    "success"true,
        
    "totalRecords"1

    And I have defined a record as;

    PHP Code:
         this.record = new Ext.data.Record.create(
                        [
                            {
    name'categories'},
                            {
    name'series'},
                            {
    name'seriesdata'}
                        ]
                        ) 
    The issue I am having, is that this data doesnt appear to load properly. I do not get any errors, just an empty store. I believe the issue has to do with the array data in the json string and somehow I am not mapping the record correctly.

    Any insights?

  2. #2
    Sencha Premium User mankz's Avatar
    Join Date
    Nov 2007
    Location
    Stockholm, Sweden
    Posts
    3,153

    Default

    What's your store definition? Set root to 'data'. Also add listeners on the store for "exception" event to notify you when a load breaks.

  3. #3
    Sencha User
    Join Date
    Jun 2009
    Location
    Burlington, Ontario
    Posts
    175

    Default

    Hey Manz,

    I've done some inspecting through Firebug, and the store is loaded. I can see the data associated with my record, however, I simply get "undefined" if I attempt to try something like store.getAt(0), I am unable to get the data into a record so I get the impression there is something wrong with my record definition.

    There are no errors through, my store is configured to catch exceptions and log them to the console.

  4. #4
    Sencha Premium User mankz's Avatar
    Join Date
    Nov 2007
    Location
    Stockholm, Sweden
    Posts
    3,153

    Default

    Ok, if you want more help you'll have to post your code (in CODE tags).

  5. #5
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    The root of a JSON store needs to be an array, but your 'data' is a single object.

    You could use:
    Code:
    root: function(value){
      return [value.data];
    }

  6. #6
    Sencha User
    Join Date
    Jun 2009
    Location
    Burlington, Ontario
    Posts
    175

    Default

    I've made some small changes to the JSON Data Structure, here is my updated store definition, record definition, etc;

    PHP Code:
    example.dao.DAOBaseCls Ext.extend(Object, {


        
    id                  null,
        
    store               null,
        
    recordConfig        null,
        
    reader              null,
        
    writer              null,
        
    root                null,
        
    entity              null,
        
    component           null,


        
    constructor : function(config) {
             
    Ext.apply(thisconfig, {
                
    autoLoad            true
             
    });
                     
            
    example.dao.DAOBaseCls.superclass.constructor.call(this);
        },

         
    /**
         * The default configuration for the Reader
         * @param Ext.data.Record
         * @return Ext.data.JsonReader
         */
        
    getReader: function(record) {

            if (! 
    this.reader) {
                
    this.reader = new Ext.data.JsonReader({ totalProperty'totalRecords',
                    
    rootthis.root,
                    
    idProperty'id',
                    
    successProperty'success'}
                        , 
    record);

            }

            return 
    this.reader;
        },

        
    /**
         * The default configuration for the Writer
         * @return Ext.data.JsonWriter
         */
        
    getWriter: function() {

            if (! 
    this.writer) {
                
    this.writer = new Ext.data.JsonWriter({
                    
    rootthis.root,
                    
    encodetrue,
                    
    encodeDeletetrue,
                    
    returnJsontrue,
                    
    writeAlLFieldstrue,
                    
    listfultrue
                
    });
            }

            return 
    this.writer;
        },


        
    /**
         * The default configuration for the Proxy
         * @return Ext.data.HttpProxy
         */
        
    getProxy: function() {

            if (! 
    this.proxy) {
                
    this.proxy = new Ext.data.HttpProxy({
                    
    api: {
                        
    read :'pp.do?xaction=read',
                        
    create'pp.do?xaction=create',
                        
    update'pp.do?xaction=update',
                        
    destroy'pp.do?xaction=destroy'
                    
    }
                });
            }

            return 
    this.proxy;
        },

        
    /**
         * The Store
         * @return Ext.data.Store
         */
        
    getStore: function() {

            if (!
    this.store) {
                
    this.store = new Ext.data.Store(
                {
                    
    id this.id,
                    
    autoSavetrue,
                    
    autoLoadthis.autoLoad,
                    
    storeIdthis.id,
                    
    readerthis.getReaderthis.getRecord() ),
                    
    writerthis.getWriter(),
                    
    proxythis.getProxy(),
                    
    successProperty'success',
                    
    baseParams: {entitythis.entitycomponentthis.componentuser_id:-1},
                    
    listeners: {
                        
    exception: function() {
                            
    console.log(arguments);
                        }
                    }
                });


            }
            return 
    this.store;
        }


    });


    example.dao.ChartDAO Ext.extend(example.dao.DAOBaseCls, {

        
    constructor: function(config) {
            
    Ext.apply(thisconfig, {
                
    root        'data',
                
    entity      'chart',  
                
    id          Ext.id(),
                
    autoLoad    false
            
    });

            
    example.dao.ChartDAO.superclass.constructor.apply(thisarguments);

            
    this.getStore();
        },


        
    /**
         * The default configuration for a record 
         *
         * @return Ext.data.Record
         */
        
    getRecord: function() {
            if (! 
    this.record) {
                
    this.record = new Ext.data.Record.create(
                        [
                            {
    name'categories' },
                            {
    name'series'}
                        ]
                        )
            }

            return 
    this.record;
        }



    }); 
    The data returning from the server is as follows;

    PHP Code:
    {
        
    "data": {
            
    "categories": [
                
    "M",
                
    "A" 
            
    ],
            
    "series": [
                {
                    
    "name""Projects" ,
                    
    "data": [
                        
    13,
                        
    20 
                    

                } 
            ]
        },
        
    "success"true,
        
    "totalRecords"

    I know that the store loads the data, as I can reference the store.reader.data.jsonData and the data is available. I am unable to get it via Ext.data.Record when using any of the store.getAt(), store.each(), etc.

    Appreciate any further help.

  7. #7
    Sencha User
    Join Date
    Jun 2009
    Location
    Burlington, Ontario
    Posts
    175

    Thumbs up [SOLVED]

    I've posted the code, requires a moderator to review. However, Condor, I changed the data to be an array and everything works beautifully.

    The new JSON Structure looks like this'

    PHP Code:
    {
        
    "data": [
            {
                
    "categories": [
                    
    "M",
                    
    "A" 
                
    ],
                
    "series": [
                    {
                        
    "name""Projects" ,
                        
    "data": [
                            
    13,
                            
    20 
                        

                    } 
                ] 
            }
        ],
        
    "success"true,
        
    "totalRecords"

    Just adding the [] around the data node, and everything works properly.

    Thanks!

Similar Threads

  1. Editing nested array in Json Store problem
    By jackygurui in forum Ext 3.x: Help & Discussion
    Replies: 20
    Last Post: 1 Apr 2014, 12:00 PM
  2. Handling data store with nested array members
    By geewhizbang in forum Ext 3.x: Help & Discussion
    Replies: 5
    Last Post: 20 Nov 2009, 12:22 PM
  3. Nested JSON Array Notation Mapping
    By Chrissu in forum Ext 2.x: Help & Discussion
    Replies: 12
    Last Post: 13 Aug 2008, 9:12 AM
  4. json data - always array of json objects?
    By shortestpath in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 20 Nov 2007, 4:37 AM

Posting Permissions

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