PDA

View Full Version : Manually add data to store with 2 associated models



SpacakCZ
5 Sep 2012, 4:01 AM
Hi,
I'm newbie with ExtJS. I have 2 models with HasMany association and a store:



Ext.define("ItemListModel", {
extend: "Ext.data.Model",
fields:[
{name:"Id"},
// some fields omitted
{name:"Name"},
{name:"NameId"}],

idProperty:"Id",
associations:[{
type:"hasMany",
associationKey:"ItemListItems",
model:"ItemListItemModel",
name:"ItemListItems"}]
});

Ext.define("ItemListItemModel", {
extend: "Ext.data.Model",
fields:[
{name:"Id"},
// some fields omitted
{name:"Name"},
{name:"NameId"}],

idProperty:"Id",
associations:[
{type:"belongsTo",model:"ItemListModel"}]
});

Ext.create("Ext.data.Store",{
storeId:"MainContent_ItemListStore",
autoLoad:true,
model:"ItemListModel",
proxy:{
data:[],
type:'memory'},
listeners:{
load
fn:function(){
LoadItemListStoreData(new SimpleQuery())
}
}
});


I have to fill data manually, due to specific AJAX request. Server response is something like this:



"ItemLists":
[{
"Id": "001",
// some fields omitted
"ItemListItems":[
{
"Id":"0001",
// some fields omitted
},
{
"Id":"0002",
// some fields omitted
}
]},
//etc ....
]


I fill store simply with


store.add(data)

where "data" is object from "ItemLists" array in response. Store is filled properly with ItemListModel records, but ItemListItemsStore (which is nested store containing ItemListItemModel records) is always empty.

So the question is how to properly fill the store so I can access records of nested model?

Thanks for your responses.

P.S.: I'm using Ext.NET v2, but I don't think this makes trouble here.

tvanzoelen
5 Sep 2012, 11:42 PM
If you use Memory, just assign the data to the data property of the proxy and call the load function of the store.

Any way, nested data must be read with a reader.