-
26 Feb 2012 5:11 AM #1
Unanswered: Issue when hasOne association is null
Unanswered: Issue when hasOne association is null
I have the following data models:
Now the problem is that some of the associations can be NULL in the received JSON data. Example: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' });
In this case if I try to access the data likeCode:"result":{ "halfTime":{ "home":3, "away":0 }, "fullTime":{ "home":4, "away":1 }, "overTime":null, "afterPenalties":null }
It results in the error:Code:var score = result.getFullTime();
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.Code:Uncaught TypeError: Cannot call method 'indexOf' of undefined
-
28 Feb 2012 1:45 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,682
- Vote Rating
- 435
- Answers
- 3111
First suggestion is you shouldn't use Ext.regModel in ST2 anymore, instead you should use Ext.define:
Running this against our latest code it only errors out when I execute getOverTime or getAfterPenalties because I don't have a url specified in a proxy on the Score model as it is trying to load the model because data isn't present.Code:Ext.define('Result', { extend : 'Ext.data.Model', config : { hasOne : [ { associatedModel : 'Score', associationKey : 'halfTime', getterName : 'getHalfTime', associatedName : 'halfTime' }, { associatedModel : 'Score', associationKey : 'fullTime', getterName : 'getFullTime', associatedName : 'fullTime' }, { associatedModel : 'Score', associationKey : 'overTime', getterName : 'getOverTime', associatedName : 'overTime' }, { associatedModel : 'Score', associationKey : 'afterPenalties', getterName : 'getAfterPenalties', associatedName : 'afterPenalties' } ] } }); Ext.define('Score', { extend : 'Ext.data.Model', config : { fields: [ {name : 'home', type : 'int', optional: true, defaultValue: null}, {name : 'away', type : 'int', optional: true, defaultValue: null} ], belongsTo: 'Result' } });Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
28 Feb 2012 11:47 PM #3
Thank you for your response. It appears that if you use a hasOne association the data really has to have a related record and can't have null. I've ended up using a hasMany association, which can accept 0 or more related record. I can then look at result.overTime().count to see if it has a overTime result or not. I would expect that similar would be possible with a hasOne association but can't get that working. For now I can live with the above.
-
24 Apr 2012 12:14 PM #4
hasOne association is null
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?
-
22 Jun 2012 12:43 PM #5
I too have the same issue. I'm using a rest proxy, and if my associated object is null, it will attempt to autoload the data via the proxy. This is an issue of course, because my business model allows in many places for null hasOne relationships, where the data doesn't exist.
To work around this for now, I am returning an empty array in the json instead of null. The model then does not attempt to autoload because it has a dummy object already loaded for the association.
Has anyone found a valid solutions to this?
-
26 Dec 2012 4:10 AM #6
I haven't yet. I am encountering a similar problem now, except the association is basically loading the first available model it finds (I added a few checks in the sencha touch 2 code to prevent it from erroring out if the referred object is null or undefined, but this seems to have introduced this new bug for me.) I'm not sure if the Sencha team will get around to this, but I think a hasOne should be able to be null.


Reply With Quote