PDA

View Full Version : Associated Model and Getter not being created



darren102
15 Sep 2012, 4:20 AM
Been working with an associated model and trying to get it to load per the Data Package docs but so far can only load the top level model. The raw content has the correct information in it hence i believe i must be doing something wrong in the model definitions hence any information would be appreciated.

ProblemCauseType Model


Ext.define('Imms.model.ProblemCauseType', {
extend:'Imms.model.BaseModel',


fields:[
{ name:'Code', type:'string'},
{ name:'Name', type:'string'}
],


associations:[
{ type:'hasMany', model:'ProblemCause', name:'Causes'}
],


proxy: {
type: 'rest',
url: 'data/problemcausetypes.json',
reader: {
type: 'json'
}
}
});


ProblemCause Model


Ext.define('Imms.model.ProblemCause', {
extend:'Imms.model.BaseModel',


fields:[
{ name:'Code', type:'String'},
{ name:'Name', type:'string'}
],


associations:[
{ type:'belongsTo', model:'ProblemCauseType', name:'CauseType'}
]
});


JSON being loaded into the Models


[
{
"__type":"DTOSerializer:#IMMSDataService.Core.DTO",
"Id":"3e3e374c-0dba-42b9-bc8f-6ff1250a5729",
"Name":"Internal",
"Code":"",
"Causes":[
{
"__type":"DTOSerializer:#IMMSDataService.Core.DTO",
"Id":"c6914a3a-d802-4220-979c-d8d36b2dde46",
"Name":"Software Failure",
"Code":"",
"CauseType":{
"__type":"DTOSerializer:#IMMSDataService.Core.DTO",
"IMMSDataModelType":"ProblemCauseType",
"Id":"3e3e374c-0dba-42b9-bc8f-6ff1250a5729"
},
"IMMSDataModelType":"ProblemCauseDetails"
},
{
"__type":"DTOSerializer:#IMMSDataService.Core.DTO",
"Id":"8f7d5674-0c91-4279-994c-f40568ff6fcb",
"Name":"Hardware Failure",
"Code":"",
"CauseType":{
"__type":"DTOSerializer:#IMMSDataService.Core.DTO",
"IMMSDataModelType":"ProblemCauseType",
"Id":"3e3e374c-0dba-42b9-bc8f-6ff1250a5729"
},
"IMMSDataModelType":"ProblemCauseDetails"
}
],
"IMMSDataModelType":"ProblemCauseTypeDetails"
},
{
"__type":"DTOSerializer:#IMMSDataService.Core.DTO",
"Id":"dc59d21c-5256-4a63-aa9d-a44f53eb2dc5",
"Name":"vl_TestProblemCauseType",
"Code":"",
"Causes":[
{
"__type":"DTOSerializer:#IMMSDataService.Core.DTO",
"Id":"7c70ceff-a001-494f-9179-03f2e4769d8c",
"Name":"vl_TestProblemCause",
"Code":"56789",
"CauseType":{
"__type":"DTOSerializer:#IMMSDataService.Core.DTO",
"IMMSDataModelType":"ProblemCauseType",
"Id":"dc59d21c-5256-4a63-aa9d-a44f53eb2dc5"
},
"IMMSDataModelType":"ProblemCauseDetails"
}
],
"IMMSDataModelType":"ProblemCauseTypeDetails"
}
]


Console output looks like this:



Array[6]


0: constructor

data: Object

Code: ""
IMMSDataModelType: "ProblemCauseTypeDetails"
Id: "36deae12-cef1-4285-8625-239d584bc1fb"
Name: "External"
__type: "DTOSerializer:#IMMSDataService.Core.DTO"
__proto__: Object

dirty: false
events: Object
hasListeners: HasListeners
id: "Imms.model.ProblemCauseType-36deae12-cef1-4285-8625-239d584bc1fb"
index: 0
internalId: "36deae12-cef1-4285-8625-239d584bc1fb"
modified: Object
phantom: false
raw: Object

Causes: Array[4]

0: Object

CauseType: Object
Code: ""
IMMSDataModelType: "ProblemCauseDetails"
Id: "be0611d6-be1d-4121-a983-44c342620286"
Name: "Vandalism"
__type: "DTOSerializer:#IMMSDataService.Core.DTO"
__proto__: Object

1: Object
2: Object
3: Object
length: 4
__proto__: Array[0]

Code: ""
IMMSDataModelType: "ProblemCauseTypeDetails"
Id: "36deae12-cef1-4285-8625-239d584bc1fb"
Name: "External"
__type: "DTOSerializer:#IMMSDataService.Core.DTO"
__proto__: Object

store: constructor
stores: Array[1]
__proto__: TemplateClass

1: constructor
2: constructor
3: constructor
4: constructor
5: constructor


Any ideas what i am doing wrong here?

darren102
15 Sep 2012, 4:22 AM
Sorry and the base model is just a base model with our regular fields in it as shown below:



Ext.define('Imms.model.BaseModel', {
extend:'Ext.data.Model',


config: {
idProperty:'Id'
},


fields:[
{ name:'Id', type:'string' },
{ name:'__type', type:'string'},
{ name:'IMMSDataModelType', type:'string'}
]
});

mberrie
15 Sep 2012, 9:34 AM
Try adding your model class and the association type (e.g. 'Ext.data.association.HasMany') as explicit dependency via 'requires'.

This fixed a similar problem for me - not 100% sure it is the same. Was too busy (and annoyed) to track it down and report as bug :)

Might also be related to this bug report (http://www.sencha.com/forum/showthread.php?242672-Ext.application-models-config-array-does-not-actually-load-models.&p=887511#post887511)

darren102
15 Sep 2012, 10:13 AM
Thanks for replying, tried your suggestion but the same issue occurred, the ProblemCauseType is populated but the ProblemCause in the Causes association are not populated even though they are in the Raw portion of the system.

I guess i have to keep digging for this one. Thank you for the suggestion though.

vietits
16 Sep 2012, 5:33 PM
You should use full classname for models when defining associations.


Ext.define('Imms.model.ProblemCauseType', {
extend:'Imms.model.BaseModel',
fields:[
{ name:'Code', type:'string'},
{ name:'Name', type:'string'}
],
associations:[
{ type:'hasMany', model:'Imms.model.ProblemCause', name:'Causes'}
],
proxy: {
type: 'rest',
url: 'data/problemcausetypes.json',
reader: {
type: 'json'
}
}
});



Ext.define('Imms.model.ProblemCause', {
extend:'Imms.model.BaseModel',
fields:[
{ name:'Code', type:'String'},
{ name:'Name', type:'string'}
],
associations:[
{ type:'belongsTo', model:'Imms.model.ProblemCauseType', name:'CauseType'}
]
});

darren102
17 Sep 2012, 4:44 AM
Works fine with that, can i suggest updating the Data Docs to let people know, since now after a second read you are not putting the model into a package just having it as User, Post etc hence the reason it worked there. Was kind of misleading, however now i know what to look for i see the reason it was not working.

Thank you for the answer.