Issue when hasOne association is null
I have the following data models:
Code:
Ext.regModel('Result', {
associations: [
{
type: 'hasOne',
model: 'Score',
associationKey: 'halfTime',
getterName: 'getHalfTime'
},
{
type: 'hasOne',
model: 'Score',
associationKey: 'fullTime',
getterName: 'getFullTime'
},
{
type: 'hasOne',
model: 'Score',
associationKey: 'overTime',
getterName: 'getOverTime',
autoLoad: false
},
{
type: 'hasOne',
model: 'Score',
associationKey: 'afterPenalties',
getterName: 'getAfterPenalties',
autoLoad: false
}
]
});
Ext.regModel('Score', {
fields: [
{name : 'home', type : 'int', optional: true, defaultValue: null},
{name : 'away', type : 'int', optional: true, defaultValue: null}
],
belongsTo: 'Result'
});
Now the problem is that some of the associations can be NULL in the received JSON data. Example:
Code:
"result":{
"halfTime":{
"home":3,
"away":0
},
"fullTime":{
"home":4,
"away":1
},
"overTime":null,
"afterPenalties":null
}
In this case if I try to access the data like
Code:
var score = result.getFullTime();
It results in the error:
Code:
Uncaught TypeError: Cannot call method 'indexOf' of undefined
If I remove the 'overTime' and 'afterPenalties' association it all works fine with the above data. I don't understand how I should add 'optional' associations.
hasOne association is null
So is there a way to avoid this error when it is a valid scenario? Some way to not have the proxy attempt to load the data if the data truly isn't there?