PDA

View Full Version : Loading nested belongsTo associations via RESTful JSON



kfarrell@cse.unl.edu
1 Dec 2011, 11:29 AM
I'm having trouble getting ExtJS 4 to load nested belongsTo associations via a RESTful JSON request. The nested hasMany associations are loaded without a problem, but in the below example, FormField.fieldDefinition does not get loaded as an associated object, and a call to getFieldDefinition() throws an error. I do, however, see the fieldDefinition object in FormField.raw, but that's not very useful.

I'm wondering if it's even possible to load nested belongsTo objects?

Any advice?





Ext.define('ABC.model.FormField', { extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'fieldType', type: 'string' },
{ name: 'required', type: 'boolean' }
],


associations: [
{
type: 'belongsTo',
model: 'ABC.model.FieldDefinition',
associatedName: 'FieldDefinition',
name: 'fieldDefinition'
},
{
type: 'belongsTo',
model: 'ABC.model.FormProcedure',
associatedName: 'FormProcedure',
name: 'formProcedure'
}
],


proxy: {
type: 'rest',
url: '/abc/server/rest/formFields',
reader: {
type: 'json',
root: 'object',
totalProperty: 'total'
}
}
});




Ext.define('ABC.model.FieldDefinition', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' },
{ name: 'label', type: 'string' },
{ name: 'description', type: 'string' },
{ name: 'type', type: 'string' }
],


associations: [
{
type: 'hasMany',
model: 'ABC.model.FieldDefinitionPossibleValue',
associatedName: 'FieldDefinitionPossibleValue',
name: 'possibleValues'
}
],


proxy: {
type: 'rest',
url: '/abc/server/rest/fieldDefinitions',
reader: {
type: 'json',
root: 'object',
totalProperty: 'total'
}
}
});




{
"success": true,
"object": {
"name": "Test Form",
"formProcedures": [
{
"procedureDefinition": {
"name": "Test Procedure",
"id": 21
},
"formFields": [
{
"fieldDefinition": {
"name": "Field1",
"label": "Field1",
"description": "",
"type": "ENUM",
"possibleValues": [
{
"label": "Could Not Administer",
"value": "Could Not Administer",
"active": true,
"id": 269
},
{
"label": "Incomplete",
"value": "Incomplete",
"active": true,
"id": 270
},
{
"label": "Complete",
"value": "Complete",
"active": true,
"id": 271
}
],
"id": 741
},
"fieldType": "combobox",
"required": true,
"id": 1
},
{
"fieldDefinition": {
"name": "Field2",
"label": "Field2",
"description": "",
"type": "TEXT",
"possibleValues": [],
"id": 742
},
"fieldType": "textfield",
"required": false,
"id": 2
}
],
"label": "Test Procedures",
"id": 1
}
],
"id": 1
}
}

bobringer
1 Jul 2012, 9:12 PM
I'm running into the same problem...

Concerned that your fears that it just doesn't work might be valid since it's 6 months later without a response.

kfarrell@cse.unl.edu
6 Jul 2012, 5:59 AM
I did manage to solve this eventually. In my case, my model class name did not match the field name in the relationship, so the JSON parser wasn't loading the associated objects. I got around this by specifying the 'associationKey' in the relationship definitions. This issue is further complicated by the use of namespaces in your application.

See these two blog posts for a good explanation:
http://extjs-tutorials.blogspot.com/2012/05/extjs-belongsto-association-rules.html
http://extjs-tutorials.blogspot.com/2012/05/extjs-hasmany-relationships-rules.html

-Kevin