PDA

View Full Version : How do you access a belongsTo associated model in a template?



xory
20 Nov 2011, 12:30 PM
I've been lurking here for a few weeks and this is my first post. Thank you all for participating in this forum. For someone learning Sencha Touch, this forum and the docs have proven invaluable.


I have a Reminder model that has a belongsTo reference to Client and ReminderType models:



App.models.Reminder = Ext.regModel('Reminder', {
fields: [
{
name: 'id',
type: 'int'
}, {
name: 'client_id',
type: 'int'
}, {
name: 'remindertype_id',
type: 'int'
}, {
name: 'duedate',
type: 'date',
dateFormat: 'n/j/Y'
}, {
name: 'body',
type: 'string'
}
],


associations: [
{ type: 'belongsTo', model: 'Client', primaryKey: 'id', foreignKey: 'client_id' },
{ type: 'belongsTo', model: 'ReminderType', primaryKey: 'id', foreignKey: 'remindertype_id' }
]


proxy: {
type: 'localstorage',
id: 're'
}
});


I'm showing a list of these reminders and I can't figure out how to access the associated objects or their fields.


I need to do something like this in the list, but I can not figure this out. I have tried looking at the model in Chrome and it's not obvious how to access these associations.



itemTpl: '{Client.name} {ReminderType.name}'


I found and tried omarc's override (http://www.sencha.com/forum/showthread.php?115541-belongsTo-association-and-XTemplate) but I can't seem to get it working. The code never fires and I don't know if that's because I'm using a list or because I'm using the localStorage proxy.


Any help or pointers would be greatly appreciated.

mitchellsimoens
20 Nov 2011, 1:21 PM
The associated model isn't reference in the data.

xory
20 Nov 2011, 1:40 PM
Thank you for your prompt reply. Do I reference it using a field/mapping?