PDA

View Full Version : Understanding belongsTo setter method



themoop
23 Apr 2012, 7:17 AM
Hi there,
I'm in need for some explanation concerning the setter of belongsTo instance.
I have a model with an association:

Ext4.define('App.model.production.Building', {
extend: 'Ext.data.Model',
idProperty: 'Id',
fields: [{
name: 'Id',
type: 'int'
}, {
name: 'Name',
type: 'string'
}],
associations: [{
type: 'belongsTo',
model: 'Dash.model.production.Type',
name: 'Type',
associationKey: 'Type',
getterName: 'getType',
setterName: 'setType'
}]
});

I also have a combobox which contains a store of types.When the combobox is selected, I do something like



//This is pseudocode
buildingModel.setType(combobox.selectedRecord);


What I expect to happen is that extjs put the foreign key AND the instance of the type record inside buildingModel but what happen is that it only put the foreign key of the type inside buildingModel and not the instance.
So when I try to do buildingModel.getType(), it try to read from the server passing the foreign key.
I already have the model, reloading it from the server is not needed so why not just set the instance of type inside buildingModel?

When loading json with associated data directly (like below), it does set the instance directly inside buildingModel and I can do buildingModel.getType() without having to go throught the server.



{
Id: 4
Name: 'Building1',
Type: {
Id: 6,
Name: 'Industrial'
}
}

Thanks

ybendana
12 Oct 2012, 3:40 PM
I ran into this same issue also documented here:

http://www.sencha.com/forum/showthread.php?236741 (http://www.sencha.com/forum/showthread.php?236741)