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
}
}
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
}
}