Sencha touch 2 download store and its associated store with embedded JSON

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?

+4
source share
2 answers

Unfortunately, this just doesn't work. This seems to be a flaw in this structure. You cannot load and save related (aggregated / nested) objects. You need to smooth your structure, and then independently link the objects in the code.

So for example ... your JSON will then ...

 { "success": true, "changes": [ { "id": 1, "changeId": "XYZ19178263", "briefDescription": "Loaded from Proxy" } ] 

}

And the second JSON file / response is as follows:

  { "success": true, "configurations": [ { "id": "3", "longName": "999-99_-_windows_7_desktop.Computer.Windows", "ciName": "999-99_-_windows_7_desktop", "change_id": 1 } ] } 
+2
source

Tinashe's answer is incorrect. Of course, you can get nested models.

However, you have your own associations regarding your JSON.

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

means changing the hasMany configurations, but you say that it belongs to it. This is the correct JSON:

  "changes": [ { "id": 1, "changeId": "XYZ19178263", "briefDescription": "Loaded from Proxy", "configuration_id": 3, "configuration": { "id": "3", "longName": "999-99_-_windows_7_desktop.Computer.Windows", "ciName": "999-99_-_windows_7_desktop" } } ] 

In any case, you need to set getterName (setterName if you want) in relation to:

 associations: [{ type:'belongsTo', model:'Changes.model.Configuration', primaryKey: 'id', foreignKey: 'configuration_id', associationKey: 'configuration', getterName:'getConfiguration' }] 

Then after loading the store, you can call myChangeModel.getConfiguration ();

+2
source

Source: https://habr.com/ru/post/1416692/


All Articles