PDA

View Full Version : Sencha-touch associate model in the same way JSON is nested



Nuthan Santharam
26 Sep 2011, 4:20 AM
Hi,


my json output is:


{"Result":
{"Data":
[{"gmt_id":"1","gmt":"-12:00","secondsDiff":"-43200","Location":"Baker Island, Howland Island"},
{"gmt_id":"2","gmt":"-11:00","secondsDiff":"-39600","Location":"American Samoa, Samoa"},
{"gmt_id":"3","gmt":"-10:00","secondsDiff":"-36000","Location":"Hawaii, Papeete"}]}}


--I want my Model to be nested with Result and Data, so that on setting autoLoad:true on the store and access gmt_id,etc on the flow. my console.log gives[ ]. i am going wrong somewhere in my model please help!!!


**--This is my Model:**
Ext.regModel('Gmt',
{'Result':
{'Data':
[
{name:'gmt_id',type:'string'},
{name:'Location',type:'string'}
]
}

});




**this is my Store to load data:**
var jsonStore = new Ext.data.Store({
model: "Gmt",
proxy: {
type: 'ajax',
url: 'gmt.php',
//url: 'data.json',
method: 'GET',
// callback: console.log(response),
reader: {
type: 'json',
//root: 'Data'
root:'Result'
// type:'json'

},
afterRequest: function (request, success) {
if (success) {
console.log("success");
} else {
console.log("failed");
}

}
},
autoLoad: true

});




**--access key:value parameters here**


jsonStore.on('load', function(){
var lstArr = new Array();
var lstAr = new Array();

jsonStore.each(function(i) {
//var gmtdata = i.data.gmt_id;
// console.log(i);
lstArr.push(i.data.gmt_id);
lstAr.push(i.data.Location);
});
console.log(lstArr);
console.log(lstAr);
/*console: [ ]*/
});

digeridoo
26 Sep 2011, 4:34 AM
Just had a quick look and I am in no way an expert...but am I right in noticing that your "gmt_id":"1" should be declared as an integer here and not a string? Or am a wrong?

{name:'gmt_id',type:'string'}

Digeridoopoo

Nuthan Santharam
26 Sep 2011, 5:08 AM
--no it didn't work...
still the same exception!!!!

Nuthan Santharam
28 Sep 2011, 10:12 PM
Hi,
Solved it myself.


--Change the Store root


--Set 'Result.Data' as root



var jsonStore = new Ext.data.Store({
model: "Gmt",
proxy: {
type: 'ajax',
url: 'gmt.php',
//url: 'data.json',
method: 'GET',
// callback: console.log(response),
reader: {
type: 'json',

root:'Result.Data'

},
afterRequest: function (request, success) {
if (success) {
console.log("success");
} else {
console.log("failed");
}




}
},
autoLoad: true


});