PDA

View Full Version : HasOne Association: Getting property by foreignID



davidcfk
21 Aug 2013, 1:26 AM
Reposted: for some reason my other post died... (admin can you assist?)

Does anybody know where I can find a good guide on how to do the following:

I have a model that has these fields
fields: [
{name: 'workorder_id', type: 'int'},
{name: 'type_id', type: 'int'},
{name: 'category_id'},
{name: 'subject', type: 'string'},
{name: 'status_id', type: 'int'},
{name: 'timecreated', type: 'string'},
{name: 'timemodified', type: 'string'},
{name: 'contact', type: 'string'},
{name: 'submitter', type: 'string'},
{name: 'responsible', type: 'string'}
]

Some of which are just ID numbers of objects I want it to have (it's essentially a hasOne). So, I have placed HasOne relationships (I'll just put an example here):
{ type: 'hasOne', model: 'AppPortal.model.workorder.Category', name: 'category_id', associationKey: 'data', foreignKey: 'category_id', getterName: 'getCategory'}

and it is meant to, when I have an instance of this Model I would be able to run something like:
modelObj.getCategory().get('name')

Assuming of course I have a Category object with the proxy set up that fetches the data etc.

Right now, for some reason, the proxy only returns the first item name, regardless of what category_id that sits in the object (interestingly the raw data property seems to keep thinking the id is 1, so obviously the loading only seems to be taking the first one, rather than looking at the category_id).

Anybody got an interesting idea on how I would be able to achieve getting the values?

davidcfk
21 Aug 2013, 5:30 PM
Well after a lot of fuss I think I ended up just including a store in the controller, and then doing a lookup... I'll be using this snippet in the renderer for my grid display I suppose.



var store = Ext.data.StoreManager.lookup('workorder.Types'); return(store.getAt(store.find('type_id', value)).get('name'));

Gary Schlosberg
23 Aug 2013, 2:51 PM
Glad to hear you found a solution. Your other thread was likely in moderation for a bit, but it was still around, until I deleted it a few minutes ago as a duplicate.