PDA

View Full Version : using ComboBox on HasOne association field



billsalvucci
4 Oct 2012, 12:18 PM
I have grid panel that is using the following data

{
'success': true,
"activities": [
{
"id": 1,
"status": "open",
"dueDate": "10/15/12",
"activityType": { "id": 1, "name": "Quote" },
"accountId": 1
},
{
"id": 2,
"status": "closed",
"dueDate": "09/15/12",
"activityType": { "id": 2, "name": "Meeting" },
"accountId": 1
},
{
"id": 3,
"status": "closed",
"dueDate": "09/15/12",
"activityType": { "id": 1, "name": "Quote" },
"accountId": 2
}
]

Activity model looks like

fields: [
{
name: 'id',
type: 'int'
},
{
name: 'status'
},
{
name: 'accountId',
type: 'int'
},
{
name: 'dueDate',
dateFormat: 'm/d/y',
type: 'date'
},
{
name: 'activityType'
}
],

hasOne: {
associationKey: 'activityType',
model: 'CrmApp.model.ActivityType'
}

ActivityType model look like

fields: [
{
name: 'id',
type: 'int'
},
{
name: 'name'
}
]

and corresponding grid column is

{
xtype: 'gridcolumn',
renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
return record.data.activityType.name;
},
dataIndex: 'activityType',
text: 'Type'
}

This works fine. The activityType.name is displayed in the column.

I now added a RowEditingPlugin and added the following editor to this column

editor: {
xtype: 'combobox',
displayField: 'name',
store: 'ActivityTypeStore',
valueField: 'id'
}

In the editor, the value of the field is "[object Object]" which makes sense since it is mapped to activityType in the data which is an object. I need to render this object to display the name just like I did to the actual column. But I don't see the ability to use a renderer with a ComboBox.
Also when I choose an item in the ComboBox, an activityType object is not created in the data, it just saves the value of the field specified in the valueField.

This seems like it should be pretty easy, but I'm not understanding how to do this.

mitchellsimoens
9 Oct 2012, 5:05 AM
Moved to Ext JS Q&A forum for you

billsalvucci
17 Oct 2012, 7:26 AM
Can't make a HasOne field an editable combobox in the UI? That is the most common scenario for a hasOne relationship. Seems like by using HasOne relationship in your model, you really limit yourself with respect to how you can use the field in the UI.
Is anyone out there actually using associations in real applications?

mitchellsimoens
17 Oct 2012, 8:49 AM
Is anyone out there actually using associations in real applications?

Yup. The hasOne and belongsTo will create a Model instance that is cached on the main model as a property, hasMany will create a store instance also cached on the main model. The associated data is then not part of the main model's data.