PDA

View Full Version : Model hasMany not working as expected



billtricarico
24 Jun 2012, 9:49 PM
Hello,

I have a Model with a hasMany association. When I instantiate the parent model, I pass in the child Model array to create the association. In the browser debugger, nothing shows up for that association.

When I use model.set() instead, it DOES set the association, but then I get the error Uncaught RangeError: Maximum call stack size exceeded

I am using ExtJS 4.1.1 RC2

Thanks.

Izhaki
25 Jun 2012, 2:06 AM
Would really help to see some code.

scottmartin
25 Jun 2012, 6:47 AM
A few thinks to check:

-Make sure you do not have any recursive/self referencing calls that would cause an infinite loop
-Check to make sure you are not calling create when you should be calling define

Scott.

billtricarico
25 Jun 2012, 5:58 PM
Ok I made a quick little test app and it led me to believe that there two different issues here.

My models:


Ext.define('MyApp.model.ParentModel', { extend: 'Ext.data.Model',
uses: [
'MyApp.model.ChildModel'
],


fields: [
{
name: 'id'
},
{
name: 'value'
}
],


hasMany: {
associationKey: 'id',
model: 'MyApp.model.ChildModel',
foreignKey: 'id',
name: 'children'
}
});

Ext.define('MyApp.model.ChildModel', {
extend: 'Ext.data.Model',


fields: [
{
name: 'id'
},
{
name: 'value'
}
]
});

The Applicatin.onLaunch():


var child1 = Ext.create('MyApp.model.ChildModel',{ id: 1,
value: 'child1'
});


var child2 = Ext.create('MyApp.model.ChildModel',{
id: 2,
value: 'child2'
});


var child3 = Ext.create('MyApp.model.ChildModel',{
id: 3,
value: 'child3'
});


var children = [];
children.push(child1);
children.push(child2);
children.push(child3);


var parent1 = Ext.create('MyApp.model.ParentModel',{
id: 1,
value: 'parent',
children: children
});


var parent2 = Ext.create('MyApp.model.ParentModel',{
id: 1,
value: 'parent'
});


parent2.set('children',children);


Issue 1: Why is it that parent1 does not get the children when I pass in children as an array, but parent2 does get children when I use set()?

Issue 2: I have the parent/child model association when I call my ExtDirect.create() method. The calls exits with "Uncaught Range Error: Maximum call stack size exceed". The stack trace is a nesting of encodeString/doEncode/encodeObject/.....

So I can work around the 1st issue (is that a bug?).
The 2nd issue I'm not sure about. Nothing is making it to the server obviously since the client side is crashing.

Thanks.

billtricarico
25 Jun 2012, 6:04 PM
Wow who downvoted me?

billtricarico
25 Jun 2012, 7:15 PM
Ok I see the problem. My child models are being extracted from a MemoryStore on a GridPanel. When I extract them as Records, they have references to their Store.

The encode process recursively creates JSON from the records, and when it sees the store, it recursively digs into the store and finds the child model records again. There is the infinite recursion.

How can I extract the model instances without it being wrapped in a record? Or do I have to just recreate the models at the minute before assigning them to the parent model?

Thanks.