PDA

View Full Version : Association Model data is not loading along with parent model



Sohil.Patel
6 Dec 2013, 8:45 PM
Hi,
This is my scenario. I have two models Parent and child. Parent model has association of child model. I am using MVC framework for loading the data. My data loading happens via controller where I call store.load() method. It loads the Parent data and only child data record node is being loaded. None of the other fields are visible in parent record.

This is my Store model configuration. Please let me know what I am missing here.

/**** Parent Model ****/
Ext.define('App.model.ParentData', {
extend: 'Ext.data.Model',
requires: [ 'App.model.ChildData'],
idProperty: 'projectId',
fields: [
{name: 'uniqueID', mapping: 'uniqueID'},
{name: 'objectID', mapping: 'objectID'},
{name: 'objectName', mapping: 'objectName'},
{name: 'reportingMth', mapping: 'reportingMth', type: 'date', dateFormat: 'Y-m-d'},
{name: 'projectName', mapping: 'projectName'},
{name: 'projectId', mapping: 'projectId'}
],
proxy: {
type: 'ajax',
api: {
read: 'data/dummy_project_masterdata.xml',
update: 'data/update_project_masterdata.xml'
},
reader: {
type: 'xml',
root: 'return',
record: 'Project'
},
writer: {
type: 'xml',
documentRoot: 'return',
record: 'Project',
writeAllFields: true
}
},
associations: [{
type: 'hasMany',
model: 'App.model.ChildData',
name: 'childdata',
associationKey: 'childData'
reader: {
type: 'xml',
record: 'childData',
root: 'Project'
}
}]
});

/*** Child Model ****/
Ext.define('App.model.ChildData', {
extend: 'Ext.data.Model',
requires: [ 'App.model.ParentData'],
idProperty: 'projectId',
fields: [
{name: 'class', mapping: '@class'},
{name: 'projectId', mapping: 'projectId'},
{name: 'initialized', mapping: 'initialized'},
{name: 'owner', mapping: 'owner'},
{name: 'role', mapping: 'role'}
],
proxy: {
type: 'ajax',
api: {
read: 'data/dummy_project_masterdata.xml'
},
reader: {
type: 'xml',
root: 'Project',
record: 'childData'
}
}
});

/*** Store ****/
Ext.define('App.store.CoreData', {
extend: 'Ext.data.Store',
requires: [
'App.model.ChildData',
'App.model.ParentData'
],
model: 'App.model.ParentData',
storeId: 'ParentData'

});

/*** Xml File being used: ***/
<return>
<Project>
<uniqueID>1</uniqueID>
<objectID>abc1</objectID>
<objectName>obj1</objectName>
<childData class="InternalClass">
<initialized>false</initialized>
<cachedSize>-1</cachedSize>
<role>role1</role>
</childData>
<projectName>ABC Project</projectName>
<projectId>Project01</projectId>
</Project>
</return>

/*Controller Call to load store */
this.getParentDataStore().load();

Parent data is getting loaded successfuly but child data doesnt get loaded. What I am missing here??

Kevin Jackson
10 Dec 2013, 1:28 PM
You would need a method to query the back-end for the associated data. By default, it loads when the foreign key is queried on the back end.This may help http://dev.sencha.com/deploy/ext-4.0.0/examples/data/associations.htmland here is the link in the Docs as wellhttp://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.association.Association

Sohil.Patel
10 Dec 2013, 4:39 PM
Thanks For your reply Kevin.

I will give it a try.

Sohil.Patel
11 Dec 2013, 5:25 PM
Hi Kevin,
I have tried what you have said but my requirement is more like this thread. This thread is mainly for Sencha touch but I am looking something similar on Ext JS. I tried what it says but not working for me. http://www.sencha.com/forum/showthread.php?115033-FIXED-583-nested-xml-complex-xml-cant-get-associations-to-work

Sohil.Patel
11 Dec 2013, 6:30 PM
Okay, here is my scenario that I would want to work.
1. I have a below xml coming from the server.
<root>
<data>
<return>
<Project>
<uniqueID>5</uniqueID>
<objectID>FF000300000005</objectID>
<objectName>Abc</objectName>
<contentMap />
<aclID>0</aclID>
<objectType>0003</objectType>
<projectStatistics>
<entry>
<long>23</long>
<com.virtua.virtuadoc.type.dao.ProjectStatistics>
<uniqueID>23</uniqueID>
<objectID>FF000F00000023</objectID>
<objectName>Test</objectName>
<contentMap />
<aclID>0</aclID>
<firstProductDate class="sql-timestamp">2014-06-30 14:00:00.0</firstProductDate>
<annualOpexUsd>26.0</annualOpexUsd>
<directAssetCosts>200.0</directAssetCosts>
<lastUpdateUserName>ADMIN</lastUpdateUserName>
<statisticsUpdateUserName>ADMIN</statisticsUpdateUserName>
<npv>10.0</npv>
<closureDate class="sql-timestamp">2018-09-08 14:00:00.0</closureDate>
<projectUniqueId>5</projectUniqueId>
<lastUpdateDatetime class="sql-timestamp">2013-08-11 14:00:00.0</lastUpdateDatetime>
<statisticsUpdateDate class="sql-timestamp">2013-01-11 14:00:00.0</statisticsUpdateDate>
<projectId>Abc1</projectId>
</com.virtua.virtuadoc.type.dao.ProjectStatistics>
</entry>
</projectStatistics>
<lastUpdateUserName>ADMIN</lastUpdateUserName>
</projectQuarterlyData>
<projectName>Abc</projectName>
<projectId>Abc1</projectId>
<currentStage>Stage1</currentStage>
<stageStatus>ON HOLD</stageStatus>
<productGroup>Group1</productGroup>
</Project>
</return>
</data>
</root>
2. I have to map this xml in the grids. Data will get updated by user and when user clicks submit button, same xml should go back to server with updated data.
3. I have created 2 stores and 2 models.
a. store1 & model1: ProjectData
b. store2 & model2: ProjectStatisticsData
4. I am loading both the store via controller as I can't use AutoLoad. Reason being I have to pass Project ID while loading the store so that particular project data should be
fetched from the database.
5. I have used hasMany association in projectData store for projectStatisticsData as there can be multiple instances of ProjectStatistics data.
6. When store.load method gets call, I can see data in both the grid but when I display the record object of ProjectData on console I could see blank store for ProjectStatisticsData.
7. I tried different combination and with one combination where I use storeConfig:{autoLoad: true} in my model association, I get data loaded for ProjectStatisticsData in ProjectData object.
But as I have to pass the projectId everytime I load the store, I can't use the autoLoad: true.
is there any otherway I could get this working?

Kevin Jackson
13 Dec 2013, 2:32 PM
I took your xml into xmlbeautifier and it says there are issues. I doubt that is your problem but likely doesn't help.

xml isn't used nearly as often as JSON as the other post mentioned so it will likely take some digging to get you pointed in the right direction. As a starter, see if you can spot any issues with your xml code.

Sohil.Patel
15 Dec 2013, 7:06 PM
Thanks for the reply Kevin. I am working on the same xml and there is no issue in reading the data. Data gets loaded into the grid perfectly fine.

I will figure it out what changes would I need to do to make it work. But just for the matter of fact, would you be able to let me know whether below link's functionality is available in Ext js? this link is for Sencha Touch.

http://www.sencha.com/forum/showthread.php?115033-FIXED-583-nested-xml-complex-xml-cant-get-associations-to-work