PDA

View Full Version : hasMany association not processing on creating



llexa313
6 Mar 2014, 4:54 AM
Hi. I create a simple model named Group, which has many groups like an example here: http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.data.association.Association



Ext.define('Group', {
extend: 'Ext.data.Model',
fields: ['id'],
proxy: {
type: 'memory',
reader: {
type: 'json'
}
},
hasMany: [{
name: 'groups',
associationKey: 'groups',
model: 'Group',
}]
});



Create an instance:


var example = new Group({
id: 0,
groups: [
{ id: 1 }
]
});


But if I call example.groups().getCount() it returns 0. Why?
Example here, console need to see output: http://jsfiddle.net/4TSDu/180/

p.szuttenbach
6 Mar 2014, 7:27 AM
You have to make sth like:

Ext.define('Group', {
extend: 'Ext.data.Model',
fields: ['id'],
proxy: {
type: 'memory',
reader: {
type: 'json'
}
},


hasMany: [{
name: 'groups',
associationKey: 'groups',
model: 'Group',
}]
});


var example = new Group({
id: 0,

});


var group = new Group({
id: 1
});


example.groups().add(group);

llexa313
6 Mar 2014, 10:47 PM
Dynamically (such as you wrote, and like some examples) I create elements without any problems (I commented this code in jsfiddle). but can I create it on constructor? Is it not convenient?

In Ext.data.Store are two methods loadData() and loadRawData(). First one working like my example (do not create associated model), but second one do it well.

I trying to understand why is it so and how can I create model objects with nested data passed as argument.

llexa313
9 Mar 2014, 10:22 PM
Anybody knows causes of this? May be it is a bug?