1. #1
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    120
    Answers
    3
    Vote Rating
    3
    clifficious is on a distinguished road

      0  

    Default Answered: Read special JSON into store

    Answered: Read special JSON into store


    Hi,

    I'm totally stuck at a point for a while now. Maybe I'm just too stupid to understand the idea behind the stores. I have following Json string
    Code:
    {  "1":[0,68,65,75],
      "2":[68,0,9,23],
      "3":[232,343,9,24],
      "4":[33,24,44,543]
     }
    And my store looks like this.
    Code:
    Ext.define('MyStore.Adjacencies', {    
        extend: 'Ext.data.Store',
        storeId: 'ThemeAdjacencies',
    
        fields: [
                 {
                     name: 'theme'
                 }],
        
        proxy: {
            type: 'ajax',
            url: 'data/Matrix.json',
            reader: {
                type: 'array',
            }
        }
    });
    But unfortunately this only reads the first line of the json. Is there an option to read the complete data?

    Thanks for any hints and helps.

  2. if you can not change the JSON response, you will need to create a custom reader, which is a bit tricky but possible.

    i recommend to extend the json one:
    http://docs.sencha.com/extjs/4.2.0/s...ta-reader-Json

    especially take a look at:
    Code:
        extractData: function(root) {
            var recordName = this.record,
                data = [],
                length, i;
    
            if (recordName) {
                length = root.length;
                
                if (!length && Ext.isObject(root)) {
                    length = 1;
                    root = [root];
                }
    
                for (i = 0; i < length; i++) {
                    data[i] = root[i][recordName];
                }
            } else {
                data = root;
            }
            return this.callParent([data]);
        }

  3. #2
    Sencha User tobiu's Avatar
    Join Date
    May 2007
    Location
    Munich (Germany)
    Posts
    2,679
    Answers
    130
    Vote Rating
    112
    tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all

      0  

    Default


    take a look at:
    http://docs.sencha.com/extjs/4.2.0/#...ta.reader.Json
    (example on top)

    most of the time, i return a JSON string like:

    Code:
    {
        "success":true,
        "data": [
            {
                "theme":...
            },
            {
                "theme":...
            }
        ]
    }
    the important part is, that you specified "theme" as a field inside your model, so the reader will search for this field in the response.

    best regards
    tobiu
    Best regards
    Tobias Uhlig
    __________

    S-CIRCLES Social Network Engine

  4. #3
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    120
    Answers
    3
    Vote Rating
    3
    clifficious is on a distinguished road

      0  

    Default


    Hi Tobi,

    I already tried changing the reader to JSON. But that just looks for the property 'theme' as intended. Unfortunately I can not change the JSON file. So I really need to read the above given JSON.
    The trick is, that it does not have a constant property to read. But that the property ("1", "2",...) is changing.

  5. #4
    Sencha User tobiu's Avatar
    Join Date
    May 2007
    Location
    Munich (Germany)
    Posts
    2,679
    Answers
    130
    Vote Rating
    112
    tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all

      0  

    Default


    if you can not change the JSON response, you will need to create a custom reader, which is a bit tricky but possible.

    i recommend to extend the json one:
    http://docs.sencha.com/extjs/4.2.0/s...ta-reader-Json

    especially take a look at:
    Code:
        extractData: function(root) {
            var recordName = this.record,
                data = [],
                length, i;
    
            if (recordName) {
                length = root.length;
                
                if (!length && Ext.isObject(root)) {
                    length = 1;
                    root = [root];
                }
    
                for (i = 0; i < length; i++) {
                    data[i] = root[i][recordName];
                }
            } else {
                data = root;
            }
            return this.callParent([data]);
        }
    Best regards
    Tobias Uhlig
    __________

    S-CIRCLES Social Network Engine

  6. #5
    Sencha Premium Member
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    120
    Answers
    3
    Vote Rating
    3
    clifficious is on a distinguished road

      0  

    Default


    Hi Tobi,

    thanks for the hint. I'll give it a try.

Thread Participants: 1

Tags for this Thread