PDA

View Full Version : Auto loaded stores and associated models: Requests are sent anyway



SanchPancho
29 Jul 2013, 6:24 AM
Hello folks,

I'm having a strange problem that causes some headache. I have two stores: "Country" and "Telecaster" which are autoloaded. The belonging models are associated, a country has many telecasters, while a telecaster belongs to one country.

Here is my code:
Country model:

Ext.define('Project.models.Country', {
extend: 'Ext.data.Model',
idProperty: 'Id',
fields: [
{name: 'Id', type: 'int'},
{name: 'Name', type: 'string'}
],
proxy: {
type: 'ajax',
url: '/projectService/country/list',
reader: {
type: 'json',
root: 'country'
}
},
hasMany: {
model: 'Project.models.Telecaster',
name: 'telecaster',
autoLoad: true,
primaryKey: 'Id',
foreignKey: 'CountryId'
}
});

Telecaster model:

Ext.define('Project.models.Telecaster', {
extend: 'Ext.data.Model',
idProperty: 'Id',
fields: [
{name: 'Id', type: 'int'},
{name: 'Name', type: 'string'},
{name: 'CountryId', type: 'int'}
],
proxy: {
type: 'ajax',
url: '/projectService/telecaster/list',
reader: {
type: 'json',
root: 'telecaster'
}
},
belongsTo: {
model: 'Project.models.Country',
getterName: 'getCountry',
setterName: 'setCountry',
primaryKey: 'Id',
foreignKey: 'CountryId'
}
});

Country store:

Ext.define('Project.stores.Country', {
extend: 'Ext.data.Store',
storeId: 'Project.stores.Country',
autoLoad: true,
model: 'Project.models.Country'
});

Telecaster store:

Ext.define('Project.stores.Telecaster', {
extend: 'Ext.data.Store',
storeId: 'Project.stores.Telecaster',
autoLoad: true,
model: 'Project.models.Telecaster'
});

Here are two samples of the auto loaded data:
Country:

{"success":true,"country":[{"Id":1,"Name":"Germany"},{"Id":2,"Name":"Taiwan"},{"Id":3,"Name":"South Africa"},{"Id":4,"Name":"Pan-Middle East"}]}

Telecaster:

{"success":true,"telecaster":[{"Id":1,"Name":"Caster 1","CountryId":1},{"Id":2,"Name":"Caster 2","CountryId":1},{"Id":3,"Name":"Caster 3","CountryId":2},{"Id":4,"Name":"Caster 4","CountryId":3},{"Id":5,"Name":"Caster 5","CountryId":4}]}

The problem I have is when executing record.getCountry() (as defined in the Telecaster model) I only get the ID and Name is empty. Then executing record.getCountry() a second time, the name is correct.
The network console in Chrome shows me a new request from Country model is sent with the country id (id) as parameter.

tl;dr:
How can I force the association to use the already auto loaded data instead of requesting the data again?

Thank you in advice.

Gary Schlosberg
6 Aug 2013, 6:40 AM
Looks like you would have to override the associations to accomplish this. As is, the framework doesn't know about other stores.