PDA

View Full Version : How to define model using associations to parse nested json array value in extjs4.1



vijayakumar84
10 Apr 2013, 11:12 PM
Hi
can anybody tell How to define model using association to parse nested json array value in extjs4.1.I am trying to parse parentName,parentAge,childId value from the below json response.I am new to mapping and association .If there is anything am wrong doing in the code let you correct me .can anybody please help in this problem

Json Response

{
"parents":{
"parent1":{
"parent":[
{
"children":{
"child":[
{
"childId":1,
"childdetails": {
"childrendetails": [
{
"childName": "Test",
"childAge": "19"
},
{
"childName": "Test1",
"childAge": "20"
}
]
}

},
{
"childId":2,
"childdetails": {
"childrendetails": [
{
"childName": "Test2",
"childAge": "25"
},
{
"childName": "Test3",
"childAge": "24"
}
]
}


}
]
}
},
{
"children":{
"child":[
{
"childId":3,
"childdetails": {
"childrendetails": [
{
"childName": "Test4",
"childAge": "25"
},
{
"childName": "test5",
"childAge": "21"
}
]
}


},
{
"childId":4,
"childdetails": {
"childrendetails": [
{
"childName": "Test6",
"childAge": "26"
},
{
"childName": "test7",
"childAge": "27"
}
]
}
}




]
},


"parentName":"firstName",
"parentAge":"28"

},


{
"children":{
"child":[
{
"childId":5,
"childdetails": {
"childrendetails": [
{
"childName": "Test8",
"childAge": "24"
},
{
"childName": "Test9",
"childAges": "27"
}
]
}


},
{
"childId":6,
"childdetails": {
"childrendetails": [
{
"childName": "Test10",
"childAge": "25"
},
{
"childName": "Test11",
"childAge": "24"
}
]
}


}
]
}
},
{
"children":{
"child":[
{
"childId":7,
"childdetails": {
"childrendetails": [
{
"childName": "Test12",
"childAge": "25"
},
{
"childName": "Test13",
"childAge": "21"
}
]
}


},
{
"childId":8,
"childdetails": {
"childrendetails": [
{
"childName": "Test14",
"childAge": "26"
},
{
"childName": "Test15",
"childAge": "27"
}
]
}
}


]
},


"parentName":"secondname",
"parentAge":"35"

}

]

}


}

}



Model


Ext.define('NestedJsonTest.model.TestJsonModel', {
extend : 'Ext.data.Model',
fields: [
'parentName', 'parentAge','childId'
],




associations: [{
type: 'hasMany',
model: 'NestedJsonTest.model.TestJsonModel',
autoLoad: true,
associationKey: 'child'
}, {
type: 'belongsTo',
model: 'NestedJsonTest.model.TestJsonModel',
associationKey: 'parent'
}]



});
Ext.onReady(function(){


TestJsonModel.load(10, {
success: function(group){
console.log(group.getTestJsonModel().get('parentName'));
console.log(group.getTestJsonModel().get('parentAge'));


group.parent().each(function(rec){
console.log(rec.get('childId'));
});
}
});



Store


[HTML]


Ext.define('NestedJsonTest.store.TestJsonStore', {
extend : 'Ext.data.Store',
storeId : 'testjsonstore',
model : 'NestedJsonTest.model.TestJsonModel',
autoLoad : 'true',
proxy : {
type : 'ajax',
url : 'test.json',
reader : {
type : 'json',
root :'parents.parent1.parent'
}
}


});





Thanks

evant
11 Apr 2013, 12:06 AM
Please stop posting questions in this forum, it's intended for discussion surrounding beta products. 4.1 has been out for over a year now.

Moving to QA.