-
26 Nov 2012 12:03 PM #1
Answered: model associations not filled with data
Answered: model associations not filled with data
Hi
I've followed the example defined here, but whatever I try I cannot get the associations to contain any data.
So, here are my stores and two models
The User modelCode:Ext.define("ModelTest.store.Data", { extend: "Ext.data.Store", config: { storeId: "usersStore", data : [ { id: 1, firstName: "Ed Spencer", products: [{id: 1, name: 'THis is a comment', user_id: 1 }] }], ], model: "ModelTest.model.User", autoLoad: true, } });
and finally the Product modelCode:Ext.define('ModelTest.model.User', { extend: 'Ext.data.Model', config: { fields: [ {name: 'id', type: 'int'}, {name: 'name', type: 'string'} ], hasMany: {model: 'Product', name: 'products'} } });
Then, from the console I didCode:Ext.define('ModelTest.model.Product', { extend: 'Ext.data.Model', config: { fields: [ {name: 'id', type: 'int'}, {name: 'user_id', type: 'int'}, {name: 'name', type: 'string'} ] } });
which returns 'products is not defined'. Any suggestions why the association is empty ?Code:Ext.getStore('usersStore').getData().each(function(i) { console.dir(i.products()); })
Cheers
-
Best Answer Posted by Tioecomp
try to use the associationKey, and on model use the complete name of the model
Code:Ext.define('ModelTest.model.User', { extend: 'Ext.data.Model', config: { fields: [ {name: 'id', type: 'int'}, {name: 'name', type: 'string'} ], hasMany: {model: 'ModelTest.model.Product', name: 'products', associationKey: 'products'} } });
-
28 Nov 2012 6:35 AM #2
try to use the associationKey, and on model use the complete name of the model
Code:Ext.define('ModelTest.model.User', { extend: 'Ext.data.Model', config: { fields: [ {name: 'id', type: 'int'}, {name: 'name', type: 'string'} ], hasMany: {model: 'ModelTest.model.Product', name: 'products', associationKey: 'products'} } });
-
29 Nov 2012 4:01 AM #3
Yes that works! Thnx. But, what is a little bit strange, is that user.products() returns a store. This store has products, but shouldn't user.products() return a collection/array of products ?
-
29 Nov 2012 4:16 AM #4
That I dont know man, I have a store returned too.


Reply With Quote