PDA

View Full Version : Nested data not initiated



jeanluca
3 Feb 2012, 5:23 AM
Hello

I have a User model which has many portfolios. Looks like


Ext.define('Owa.model.Client', {
extend: 'Ext.data.Model',
alias: 'model.Client',
config: {
fields : [ 'id', 'name'],
hasMany: {model: 'Portfolio', name: 'portfolios'}
}
});
The Porfolio model:

Ext.define('Owa.model.Portfolio', {
extend : 'Ext.data.Model',
alias : 'model.Portfolio',
config: {
fields : [ 'id', 'name' ],
belongsTo: 'Owa.model.Client'
}
});
And the store (with the data):

var data = {
clients: [
{
name: 'James',
id: 100,
portfolios: [
{
name: 'S001',
id: 21,
user_id: 100
},
{
name: 'S002',
id: 22,
user_id: 100
},
{
name: 'S003',
id: 23,
user_id: 100
}
]
}]
};


Ext.define('Owa.store.Clients', {
//storeId: 'clients',
//requires : ['Owa.model.Client', 'Owa.model.Portfolio'],
extend: 'Ext.data.Store',
config: {
model: 'Owa.model.Client',
data : data,
autoload: true,

proxy: {
type: 'memory',
reader: {
type: 'json',
rootProperty: 'clients'
}
}
}
});
Now, when I inspect the 'store' I can see the clients but not the HasMany association: So when I do

store.getAt(0).get('portfolios') I get undefined. I followed this (http://docs.sencha.com/touch/2-0/#!/api/Ext.data.reader.Reader) doc. Any suggestions why the association isn't working ?

Thanks a lot
Luca

mitchellsimoens
3 Feb 2012, 7:54 AM
When you use an association, the data isn't on the model. A getter method is created and should be used on the model.

jeanluca
3 Feb 2012, 8:36 AM
thanks for the response!
So you mean this


store.getAt(0).portfolios()

This however gives


Uncaught TypeError: Object [object Object] has no method 'portfolios'

I can see that store.getAt(0).$className is "Owa.model.Client" and if I inspect the Object


Ext.apply.create.Class

_data: Object
data: Object

id: 100
name: "James"
__proto__: Object

id: "ext-record-1"
internalId: 100
modified: Object
raw: Object

id: 100
name: "James"
portfolios: Array[3]

0: Object

id: 21
name: "S001"
__proto__: Object

1: Object
2: Object
length: 3
__proto__: Array[0]

__proto__: Object

stores: Array[1]
__proto__: TemplateClass
Any other suggestions ?

jeanluca
3 Feb 2012, 8:47 AM
found the problem. It was on the line of the hasMany. The model referenced needed the full path!

TommyMaintz
6 Feb 2012, 12:25 PM
Glad to see you have found it :) In general its always best to use full class names as this will cause you the least confusion. Only in stores, models, views and controllers configuration in your Application definition when using our MVC package will it automatically prepend your application name space for you automatically.

jeanluca
6 Feb 2012, 11:08 PM
ok, good to know! Thnx

caroparo
20 Feb 2012, 7:53 AM
Hi, I'm doing exactly the same thing (inlined data, nested model, memory proxy, json reader) using touch2-b3. However in my case (I dumped from the store's load event handler):
- store.first().data is an Object with the right fields and the hasMany subarray Array[0] named 'entries'
- store.first().entries is undefined
- I see Array[3] as defined in store.first().data.raw Object too.

I guess it's hard to tell without the actual code, but I'm wondering if anyone's seen similar?

Update:
- Adding associationKey: 'entries' gets me the Array[3] into store.first().data instead of Array[0].
- store.first().get('entries') gives me the Array[3]

ratmat2000
6 Mar 2012, 11:11 PM
Thank you for this answer ! I spend hours trying to figure out why nested data wasn't loading. The associationKey was the issue!