PDA

View Full Version : Nested objects in a model?



rossmurphy
20 Dec 2011, 2:34 AM
I receive the below from an api call. Is it possible to use nested objects in a model? If so, how would I use the regModel to register the below hierarchy?

Thanks.


{
"Store": {
"id":"1721",
"name":"The Widget Store",
"address_1":"91 Widget Rd",
"address_2":"-",
"marker":"default_marker.png",
"lat":"-33.8868",
"lng":"151.177"
},
"Suburb": {
"name":"Widget Land"
}
}




Ext.regModel('Store', {
fields: ['id', 'name', 'address_1', 'address_2', 'marker', 'lat', 'lng', 'suburb']
});

mitchellsimoens
20 Dec 2011, 5:17 AM
You won't be able to easily. You have to go to the reader, use the createAccessor method like it does for the total property in order to get that.


reader.getSuburb = reader.createAccessor('Suburb');

Now the reader will have a getSuburb method that you can execute passing the raw data.

skirtle
20 Dec 2011, 5:40 PM
You could jump through some hoops with mappings. It's a bit unpleasant though.


Ext.define('MyModel', {
extend: 'Ext.data.Model',

fields: [
{name: 'id', mapping: 'Store.id'},
{name: 'name', mapping: 'Store.name'},
...
{name: 'suburb', mapping: 'Suburb.name'}
]
});

Ext.regModel is deprecated, use Ext.define. Note also that models are just classes and live in the global namespace, so you should really namespace them just like you would any other class.