PDA

View Full Version : Store model and Association with a JSON Map



plgagnon
10 Aug 2016, 8:18 AM
Hello,

I have been wondering how can I associate this data:




rows: [
{
customerId: 1,
customerName: 'customer1',
forecasts: {
'Y1': {
currentValue: 1,
currentPercent: 99
},
'Y2': {
currentValue: 1,
currentPercent: 99
}, etc
}
}, etc



All I have for now is this and Im kinda stuck...




Ext.define('armed.model.Forecast',
{
extend: 'Ext.data.Model',
fields: [
{name: 'currentValue'}, // ???? Probably not good...
{name: 'currentPercent'} // ???? Probably not good...
]
});

Ext.define('app.model.Customer',
{
extend: 'Ext.data.Model',
...
fields: [
{name: 'customerId'},
{name: 'customerName', type: 'string'}
],
hasMany: [
{
name: 'forecasts',
model: 'app.model.Forecast',
associationKey: 'forecasts'
}
]
});



Since forecasts is a map ('Y1', 'Y2', etc) of value generated by server, I cannot be sure exactly what will be these values.. No idea how to map this.

plgagnon
11 Aug 2016, 3:37 AM
fixed some error in my example, still need help! :(

jdkhamba
20 Aug 2016, 6:02 PM
forecasts needs to be an array since it is a nested. So you need to convert that to an array:


var forecastArr =[];for(var prop in forecasts){forecastArr.push(forecasts[prop])}

It would be even better if you would do it on the server and just present it in the way client expects it.