PDA

View Full Version : Getting a field data from Parent model inside child model using belongsTo Assosiation



sarasa
8 Nov 2013, 5:19 AM
Hi,

I have a data like following


{
"name": "ABC",
"type": "automobile",
"items": {
"uri": "items",
"collection": [
{
"category": {
"name": "First Category",
"automobile": "50cc"
}
}
]
}
}

I have a parent model:





Ext.define('App.model.Product',
{
extend: 'Ext.data.Model',
requires: [
'App.model.Category',

],
idProperty: 'name',
fields: [
'name',
'type'
],
hasMany: [
{
model: 'App.model.Category',
name: 'objects',
associationKey: 'items.collection'
}
],
proxy: {
type: "rest",
format: "json",
url: 'myurl'
},
autoSync: true,

});


Child model:





Ext.define('App.model.Category', {
extend: 'Ext.data.Model',
belongsTo: [
{
name: 'products',
instanceName: 'products',
getterName: 'getProducts()',
setterName: 'set',
model: 'App.model.Product',
primaryKey:'name'
}
],
fields: [
{
name: 'name',
mapping: 'category.name'
},
{
name: 'listtype',
mapping: 'category',
convert: function(object, model) {
//here object returns as{ "name": "First Category", "automobile": "50cc" }
//I want to some how get the parent's models type parameter which is automobile and get it here as
// var type=model.getCategory().get('type')- but this is always null.
var listtype=object[type];


return listtype;
}
}
],




});


Basically, i want to get the type from parent model (say automobile in this case) and map if child model has that field and return as listtype.

How can I get parent model's type value from child model?
Pls help