Jom
Jom

Reputation: 1897

Binding json data into store ubsing extjs

i have json data like this

{"GetStudentDetails":
    {"TotalCount":5,
    "RootResults":[
    {"city":"West Chester","country":"USA","state":"PA ","student_id":100},
    {"city":"Philly","country":"USA","state":"PA","student_id":101},
    {"city":"Buffalo","country":"USA","state":"NY","student_id":102},
    {"city":"Naigra City","country":"USA","state":"NY","student_id":103},
    {"city":"West Chester","country":"USA","state":"PA","student_id":104}]
    }
}

How to get this data into a store? i am trying using a model like this.

Ext.define('User', {
                extend: 'Ext.data.Model',
                fields: [
                    { type: 'string', name: 'TotalCount' }
                ],
                hasMany: [{ model: 'RootResults', name: 'RootResult'}]
            });

            Ext.define("RootResults", {
                extend: 'Ext.data.Model',
                fields: [
                    { type: 'string', name: 'city' },
                    { type: 'string', name: 'country' },
                    { type: 'string', name: 'state' },
                    { type: 'string', name: 'student_id' }
                ],
                belongsTo: 'User'
            });

            var store = Ext.create('Ext.data.Store', {
                model: 'User',
                proxy: {
                    type: 'ajax',
                    url: 'users.json',
                    reader: {
                        type: 'json'
                    }
                }
            });

How should my model be? when i am giving some more simple json i am getting the store loaded. i think the problem is with mapping?

Upvotes: 0

Views: 958

Answers (1)

sha
sha

Reputation: 17860

Define model as

Ext.define("RootResults", {
                extend: 'Ext.data.Model',
                fields: [
                    { type: 'string', name: 'city' },
                    { type: 'string', name: 'country' },
                    { type: 'string', name: 'state' },
                    { type: 'string', name: 'student_id' }
                ],
            });

And inside the reader definition add two parameters:

root: 'GetStudentDetails.RootResults'
totalProperty: 'GetStudentDetails.TotalCount'

Something like that... Main idea - don't try to bring internal JSON structure to your model - it should be reader responsibility to properly parse it.

Upvotes: 1

Related Questions