PDA

View Full Version : Grid column -> Model association (belongsTo - one to many)



richiejaeger
11 Apr 2013, 1:12 PM
Hey,

i got a problem and i just want to know if i am using the association in my model correct:

owner model

Ext.define('Admin.model.Relation', {
extend: 'Ext.data.Model',

fields: [{
name: 'relation_id',
type: 'int'
},{
name: 'relation_item_type',
type: 'string'
},{
name: 'relation_item',
type: 'int'
},{
name: 'relation_meta',
type: 'int'
}],

idProperty: 'relation_id',

associations: [{
type: 'belongsTo',
model: 'Admin.model.Meta',
primaryKey: 'meta_id',
foreignKey: 'relation_meta',
getterName: 'getMeta'
}],


proxy: {
type: 'ajax',
api: {
create: 'relations/create',
read: 'relations/read',
update: 'relations/update',
destroy: 'relations/destroy',
},
reader: {
type: 'json',
root: 'items',
totalProperty: 'total',
successProperty: 'success',
},
writer: {
type: 'json',
writeAllFields: true,
encode: true,
root: 'items'
}
}
});

associated model

Ext.define('Admin.model.Meta', {
extend: 'Ext.data.Model',

fields: [{
name: 'meta_id',
type: 'int'
},{
name: 'meta_type',
type: 'string'
},{
name: 'meta_value',
type: 'string'
},{
name: 'meta_item_type',
type: 'string'
},{
name: 'meta_type_text',
type: 'string'
}],

idProperty: 'meta_id',

proxy: {
type: 'ajax',
api: {
create: 'metas/create',
read: 'metas/read',
update: 'metas/update',
destroy: 'metas/destroy',
},
reader: {
type: 'json',
root: 'items',
totalProperty: 'total',
successProperty: 'success'
},
writer: {
type: 'json',
writeAllFields: true,
encode: true,
root: 'items'
}
}
});

grid

Ext.define('Admin.view.relation.Grid' ,{
extend: 'Ext.grid.Panel',
alias : 'widget.relation-grid',
id: 'relationGrid',
store: 'Relations',

initComponent: function() {
Ext.apply(this, {
border: false,
dockedItems: [{
xtype: 'toolbar',
items: [{
iconCls: 'fam add',
itemId: 'add',
text: 'Relation hinzufügen',
action: 'add'
}]
},{
xtype: 'pagingtoolbar',
dock: 'bottom',
store: 'Relations',
displayInfo: true
}],


columns: [{
header: 'ID',
hidden: true,
dataIndex: 'relation_id',
flex: 0
},{
header: 'Titel',
flex: 2,
dataIndex: 'relation_meta',
renderer: function (value, metaData, record, row, col, store, gridView) {
record.getMeta({
success: function(profile, operation) {
console.log(profile.get('meta_value'));
return profile.get('meta_value');
}
});
},
}]
});

this.callParent(arguments);
}
});


I am calling the getter in the column renderer function and i can see also an request in my network tab: .../metas/read?_dc=1365714150006&id=118

But why id=118? Shouldn't it be meta_id? So far no problem, i am also returning 1 item.

In the console i can see the correct value via "console.log" in the renderer function, but the grid column is empty.

Is someone able to help me? :)

Thanks!

richiejaeger
12 Apr 2013, 11:55 AM
Push, sorry guys i really need help. It wont work :(

richiejaeger
14 Apr 2013, 1:15 AM
:(

Is it not possible to show those data in a grid?

sebastian-julius
26 Apr 2013, 1:21 AM
The only way I found out it works is to use mapped fields in your parent model.



fields: ['id',
{
name: 'relatedValue',
mapping: 'meta.metaValue'
}


Find more on this topic here: http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.data.Field-cfg-mapping