I started creating the Sencha Touch 2 application, which defines two models: “Change and customization”. A Change belongs to the configuration. Both models also have a store setup. In the change repository, I have a proxy setting for requesting data that is returned in JSON format. JSON data has a change with a nested configuration. The change loads just fine, but when I try to get the appropriate configuration from the Change instance, it does not work.
I defined the models as follows:
Change the model:
Ext.define('Changes.model.Change', { extend: 'Ext.data.Model', xtype: 'changemodel', config: { fields: [ {name:'id', type:'int'}, {name:'changeId', type:'string'}, {name:'briefDescription', type:'string'}, {name:'configuration_id', type:'int'} ], associations: [{ type:'belongsTo', model:'Changes.model.Configuration', primaryKey: 'id', foreignKey: 'configuration_id', associationKey: 'configurations' }], }, });
Configuration Model:
Ext.define('Changes.model.Configuration', { extend: 'Ext.data.Model', xtype: 'configurationmodel', config: { fields: [ { name: 'id', type: 'int'}, { name: 'longName', type: 'string' }, { name: 'ciName', type: 'string' }, ], hasMany: {model: 'Change', name: 'changes'} } });
Each model has storage.
Modifies the repository:
Ext.define('Changes.store.Changes', { extend: 'Ext.data.Store', requires: 'Changes.model.Change', config: { model: 'Changes.model.Change', proxy: { type: 'ajax', url: 'services/changes.php', reader: { type: 'json', rootProperty: 'changes' } }, sorters: [{ property: 'briefDescription', direction: 'ASC'}], } });
Saving configurations:
Ext.define('Changes.store.Configurations', { extend: 'Ext.data.Store', requires: ['Ext.data.proxy.LocalStorage'], config: { model: 'Changes.model.Configuration', grouper: { sortProperty: "ciName", direction: "DESC", groupFn: function (record) { return record.get('ciName')[0]; } }, proxy: { type: 'ajax', url: 'services/configurationItems.php', reader: { type: 'json', rootProperty: 'configurationItems' } } } });
My JSON, which is returned from services/changes.php
, looks like this:
{ "success": true, "changes": [ { "id": 1, "changeId": "XYZ19178263", "briefDescription": "Loaded from Proxy", "configuration_id": 3, "configurations": [ { "id": "3", "longName": "999-99_-_windows_7_desktop.Computer.Windows", "ciName": "999-99_-_windows_7_desktop" } ] } ] }
In the browser console, I can execute the following commands:
Changes.myStore = Ext.getStore('Changes'); Changes.myStore.load(); Changes.record = Changes.myStore.findRecord('id', '1'); Changes.record.getAssociatedData()
The last command will return an object with a Configuration object inside, but all field values show null
, except for id
, which seems to be set to a random value:
Object Configuration: Object ciName: null id: "ext-record-11" longName: null
Can anyone understand why the nested instance of Configuration
is not saved in my JSON? And should a nested Configuration
instance in JSON be automatically added to the Configurations
repository?