PDA

View Full Version : Unable to load hasMany association



Manoj Wadhwa
10 Dec 2011, 4:27 PM
I'm experiencing problems when loading data using hasMany association. Here is my code. The problem is that using firefox I can see that no record.categories() function is created for the record and thus the third column in the grid doesn't display. Please let me know what is being wrong with this code.


Ext.define('Ads.model.Application', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string'}
],
hasMany :
{
model: 'Ads.model.Category',
name: 'categories',
associationKey: 'categories',
primaryKey: 'id',
foreignKey: 'application_id',
autoLoad: true
}


});

Ext.define('Ads.model.Category', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'displayName', type: 'string'}
],
belongsTo : 'Ads.model.Application'
});

//server response
{
"success": true,
"results": [
{
"id": 1,
"name": "app 1",
"categories": [
{
"id": 1,
"displayName": "cat 1",
"application_id" : 1
},
{
"id": 2,
"displayName": "cat 2",
"application_id" : 1
}
]
}
]
}

//code to make store and display
var store = Ext.create('Ext.data.Store', {




model: 'Ads.model.Application',
storeId: 'appStore',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'app/data/applications.json',
reader: {
type: 'json',
root: 'results',
successProperty: 'success'
}
}
});


var applicationsGrid = Ext.create('Ext.grid.Panel', {
title : 'Applications',
store: store,
columns: [
{header: 'Id', dataIndex :'id'},
{header: 'Name', dataIndex :'name', flex: 1},
{
header: 'Categories',
dataIndex: 'id' ,
renderer: function(value, metaData, record, rowIndex){
var out = '';
var categories = record.categories();
categories.each(function(category){
out = categories[i].displayName + " ";
})
return out;
}


}
]
});

mitchellsimoens
12 Dec 2011, 12:07 PM
Here is an example I made earlier this year...