-
28 Nov 2012 4:18 PM #11
tried this also - didn't work for me...
-
28 Nov 2012 5:06 PM #12
Hi,
Now Quite exasperated! - I remember this problem in Sencha 1.x and having to do the same thing.
I ended up removing all the associations from the models (both sides - 'has many' and 'belongs to' ) and as suggested in 'JRS' post copying the raw data to the model/store data - crazy stuff!
e.g.
Code:callback: function (records, operation, success) { console.log('raw'); console.log(records[0].raw); // copy raw to data as sencha model load with association for nested JSON doesnt work records[0].data = records[0].raw console.log('records'); console.log(records);
-
28 Nov 2012 5:15 PM #13
Comment
Comment
Hi,
It seems there some issues with the models and associations not working with even simple nested data examples - these issues have been around in the posts since Sencha 1.x.
Or perhaps its a documentation issue and we are trying to use the models as was not intended?
My understanding is that it is effectively a client side object structure (in Sencha Javascript) for data retrieved from the server e.g. a simple db entity framework on the client.
Have I got this wrong?
It would be great if someone from Sencha could provide some guidance on how this is supposed to work e.g. is nested JSON data supported - if so how?
Also there seems to be lots of different posts out there on the correct plumbing for models to make this work.
e.g.
- the need for associationKeys in various formats - or not
- needing to have an 'id' field on every model - or not
- needing to relate the models with foreign keys with the same model name / naming std as the parent - or not.
NOTE: this last requirement seems strange to me given the relationships are defined by the nesting in the JSON format e.g. why would you need to do this as JSON defines parent/child relationships for you via the nature of its format.
Anyway END RANT - if anyone can point me to a definitive resource on how this all hangs together (for an enterprise scenario e.g. not simple examples we have now ) then mea culpa.

-
28 Nov 2012 7:28 PM #14
I am sorry ,the records in callback is array, not collection.
Code:callback:function(records,operation, success){ var user0,user0_role0,user0_message0; //user0 = records.getAt(0); user0 = records[0]; user0_role0 = user0.roles().getAt(0); user0_message0 = user0.messages().getAt(0); //for each user0 messages user0.messages().each(function(message){ // do something here }) }I write English by translator.
-
28 Nov 2012 8:02 PM #15
thanks - doesnt do anything unfortunately e.g. no nested data returned..and never reaches '4' below...see attachment also
Code:userStore.load({ callback: function (records, operation, success) { console.log('1'); //user0 = records.getAt(0); user0 = records[0]; user0_role0 = user0.roles().getAt(0); user0_message0 = user0.messages().getAt(0); console.log('2'); console.log(user0); console.log('2.1'); console.log(user0_role0); console.log('2.2'); console.log(user0_message0); console.log('3'); //for each user0 messages user0.messages().each(function (message) { // do something here console.log('4'); console.log(message); })
-
28 Nov 2012 8:13 PM #16
I create a demo ,everything works fine.(st2.1)
I write English by translator.
-
28 Nov 2012 10:55 PM #17
It seems like the variable records is already your user. As I can see this class also contains a rolesStore and messagesStore so user0.Roles() should return all the roles and user0.Messages() should return the messages.
-
29 Nov 2012 4:07 PM #18
Hi haduki,
thanks!
yes now working (sort of - although there seem to be some strange ways I need to access things - do things - see my questions below)
It turns out these are the magic lines (in red below) that make everything work , and also how I was accessing the 'roles()' method when there was only an empty role array passed... I have some questions below to aid in my understanding of how this should all hang together.
My questions:Code:{model: 'HMMobileCharts.model.Role', name: 'roles', associationKey: 'Roles' } , { model: 'HMMobileCharts.model.Message', name: 'messages', associationKey: 'Messages' }
1/
what does the 'associationKey' do and what is the rules around this - naming etc.
Also I saw this tweet https://twitter.com/SenchaMitch/stat...94918567976962
but don't understand what this means.
2/
It turns out I have to access my fields like this (below) - always using the keyword 'data' - is this correct?
and for nested:Code:var user = records[0]; console.log(user.data.FirstName); console.log(user.data.LastName); console.log(user.data.IsAuthenticated);
3/Code:messages.getAt(0).data.MessageStr
if the JSON returns an empty role array e.g. "Roles":[] then it seemsreturn nothing - is the best way to test for this having values as follows? -Code:user.roles()....
Code:if(roles.data.length>0)
4/
instead of seeing (as I would of expected) the 'user' object have nested objects arrays for roles and messages (as shown in the 'raw' value) they appear to come accross in a 'messageStore' and 'userStore' - does someone have an explanation of this and/or can point me to some doco on this?
..
It would be great if there was a very clear and detailed guide somewhere about how all this works/hangs together with more than just trivial examples - this would save a lot of time and effort.
If anyone knows of such a thing I would be keen to know.
thanks to everyone who responded!
..
-
30 Nov 2012 2:06 AM #19
1/ sorry for my poor english,i cant say long sentence.
you just need to know:
2/ no.Code:dataFromServer={users:[{ roles:[{id:'role_1'}]}]} associationKey should be 'roles' dataFromServer={users:[{ myRoles:[{id:'role_1'}]}]} associationKey should be 'myRoles' dataFromServer={ users:[ { id:'user_1', extraData:{roles:[{id:'role_1'}]}, lastName:'FirstName', ... } ] } associationKey should be 'extraData.roles'
3/Code:user.get('FirstName'); user.get('LastName'); user.messages().getAt(0).get('MessageStr')
4/Code:if(user.roles().getCount()===0)
it is let you control the entity easily.
if the roles and messages are read-only in your application,you no need to use association.
http://docs.sencha.com/touch/2-1/#!/...on.Association
http://docs.sencha.com/touch/2-1/#!/guide/models
http://docs.sencha.com/touch/2-1/#!/guide/storesI write English by translator.
-
1 Dec 2012 10:30 PM #20
thanks again for your reply.
the std documentation is not very clear on all of these aspects
but thanks anyway!



Reply With Quote