1. #1
    Sencha User
    Join Date
    Aug 2011
    Posts
    4
    Vote Rating
    0
    nahun is on a distinguished road

      0  

    Default Answered: load nested rows in model

    Answered: load nested rows in model


    I've got nested rows due to the way an API sends back JSON, I can't change the way it replies.

    The problem is the model isn't loading it correctly and I can't use the data. Below is example JSON:

    Code:
    {
        "id":"list",
        "jsonrpc":"2.0",
        "result": [
            [
                [
                    {
                        "id":"1",
                        "length":"60000000",
                        "connections":"0",
                        "dir":"/home1"
                    }
                ]
            ],
            [
                [
                    {
                        "id":"2",
                        "length":"50000000",
                        "connections":"0",
                        "dir":"/home2"
                    }
                ]
            ]
        ]
    }
    The "result" field would be the root for the reader, but I don't know how to create the model to read the fields or if I need to use hasMany somehow. I can see the store has 2 items, but nothing in them.

    Thanks

  2. Try this:

    Code:
    reader: {
        type: 'json',
        root: 'result',
        extractData: function(root) {
            var data = [], length = root.length, i = 0;
    
            for (; i < length; i++) {
                if (root[i][0].length) {
                    data.push(root[i][0][0]);
                }
            }
            
            return Ext.data.reader.Reader.prototype.extractData.call(this, data);
        }
    }

  3. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,585
    Answers
    540
    Vote Rating
    321
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    If the config option record behaved the same way as root this would be easy, you'd just set record: '[0][0]'. But it doesn't.

    One way is to do a little overriding:

    Code:
    reader: {
        root: 'result',
        type: 'json',
        extractData: function(root) {
            var data = [], length = root.length, i = 0;
    
            for (; i < length; i++) {
                data[i] = root[i][0][0];
            }
    
            return Ext.data.reader.Reader.prototype.extractData.call(this, data);
        }
    }

  4. #3
    Sencha User
    Join Date
    Aug 2011
    Posts
    4
    Vote Rating
    0
    nahun is on a distinguished road

      0  

    Default


    This is working great, thanks!

    The only issue I have is if an entry is empty. The extractData function always fills in the data array with that undefined entry no matter what I do. Here is example JSON

    Code:
    {
        "id":"list",
        "jsonrpc":"2.0",
        "result": [
        [
            [ ]
        ],
        [
                [
                    {
                        "id":"1",
                        "length":"60000000",
                        "connections":"0",
                        "dir":"/home1"
                    }
                ]
            ]
    }
    And a modified version of that function. Mostly the if statement testing if root[i][0] length is > 0 is important. But the console.log shows var data already has the data in it and the model complains about the field being undefined. If the empty entry is last, this doesn't happen though.

    Code:
    reader: {
        type: 'json',
        root: 'result',
        extractData: function(root) {
            var data = new Array();
                    console.log(data);
            var length = root.length, i = 0;
            for (; i < length; i++) {
                if(root[i][0].length > 0) {
                    data[i] = data[i][0][0];
                }
            }
            
            return Ext.data.reader.Reader.prototype.extractData.call(this, data);
            
        }
    }
    I've tried using root[i][0][0] !== undefined instead of testing the length, but no difference.

  5. #4
    Sencha User
    Join Date
    Aug 2011
    Posts
    4
    Vote Rating
    0
    nahun is on a distinguished road

      0  

    Default hokey success

    hokey success


    So I got it to work by looping through data afterwards and splicing out the undefined field. Seems like a hokey way to do it though.

    Code:
    reader: {
        type: 'json',
        root: 'result',
        extractData: function(root) {
            var data = [], length = root.length, i = 0;
            for (; i < length; i++) {
                data[i] = root[i][0][0];
            }
            for (i = 0; i < length; i++) {
                if(data[i] === undefined) {
                    data.splice(i,1);
                }
            }
            return Ext.data.reader.Reader.prototype.extractData.call(this, data);
        }
    }

  6. #5
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,585
    Answers
    540
    Vote Rating
    321
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    Try this:

    Code:
    reader: {
        type: 'json',
        root: 'result',
        extractData: function(root) {
            var data = [], length = root.length, i = 0;
    
            for (; i < length; i++) {
                if (root[i][0].length) {
                    data.push(root[i][0][0]);
                }
            }
            
            return Ext.data.reader.Reader.prototype.extractData.call(this, data);
        }
    }

  7. #6
    Sencha User
    Join Date
    Aug 2011
    Posts
    4
    Vote Rating
    0
    nahun is on a distinguished road

      0  

    Default


    Works perfectly, thanks skirtle!

Thread Participants: 1

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi