PDA

View Full Version : Convert Store with Nested Stores to Plain Old JavaScript Object (or JSON)



kivras
20 Nov 2012, 9:50 AM
Although i have achieved the desired functionality i am in search of the recommended best practice.

I have this JSON


{
"sessionDetails":{
id:"1804",
sessionDate:"10-02-2010",
duration:"185",
totalCost:"18.55",
partsCost:"10",
laborCost:"8.55",
description:"Alagi kadenas xronismou, Alagi ladion , zigostathmisi kai alla polla",
parts:[
{id:"43",name:"kadena xronismou",price:"50",quantity:"1"},
{id:"45",name:"Lasxtixa",price:"20",quantity:"2"},
{id:"05",name:"Motul 1L 15/40",price:"12",quantity:"4"}
]
}
}


The Parts Model is correctly associated with SessionDetails Model using the HasMany property.

So when is get the data from the JSON i get a
Store (with 1 sessionDetails record) containing another Store with Part records.

When i call sessionDetailsStore.first().getData() i get only the primary node data
(all fields not the array. Also i used firebug and the Parts array is on a completely different node unrelated with the JSON structure).
The Parts Store is accessible via calling sessionDetails.parts().

Finally i want to convert all data into a POJO in order to use it on a XTemplate.

My solution is this.



var detailsRecord = SessionDetailsStore[0] (The parent Store)
var detailData = detailsRecord.getData();

detailData.parts = new Array();
var partsArr = detailsRecord.parts().getRange();
for(var i=0; i<partsArr.length;i++){
detailData.parts[i] = partsArr[i].getData();
}


Shouldn't be a way of converting the store to it's primitive data-form using a simple command?

mitchellsimoens
22 Nov 2012, 6:20 AM
You are doing it properly. This isn't normally done as vast majority don't need to.