0
votes

Is this a valid model ? I doesn't seem to be working ? Maybe I have to use a HasOne, but the problem is that it has an association key and my data does not.

    {name:'name', type: 'string'},
    {name: 'desc', type: 'string', defaultValue: 'NA'},
    {name:'item', fields: [
        {name: 'name'},
        {name: 'desc'},
    ]}

In the above i am using name Item with another array of fields. Maybe this isn't correct? Is the above the right syntax ?

if I have to use HasOne association it appears that I need provide an association key but my data doesn't have an association key. Here is an example JSON record.

{"name" : "test item",
"desc" : "my temporary desc"
"item" : [ 
          { "name" : "item 1", 
            "desc" : "item 1 desc" 
          } 
         ] 
 }

As you can see from the above, I have the main item "test item" and it arrives with 1 child, but the child has no association id i.e. child to parent. no hay id is present, its just a json complex object.

I am really struggling with this. Any ideas ?

I am using a rest proxy and and reader is 'son'

Any ideas ?

A little confused. HasOne does sound ideal BUT i have no relationship there.

Thanks in advance

1

1 Answers

1
votes

You don't need an association id. To achieve what you desire you need to provide two models. One for the parent and one for the "hasone" children. You also need to specify "how" to find the data of the child /children within the parent model, something like this:

 Ext.define('User', {
     extend: 'Ext.data.Model',
     fields: [{name:"fullName",type:"string"}],
     hasOne: [{name: 'address', model: 'Address', associationKey: 'address',getterName: 'getAddress',                          associatedName: 'address'}]
 }); 

Ext.define('Address', {
     extend: 'Ext.data.Model',
     fields: ['city','state']
});

The assosciationKey is very important, because it tells extjs where to find the data for the Address model, within the data for the user. Using the getterName attribute you are able to specify the function with which you can retrieve your child.

The passed data needs to look somewhat like this:

store.loadRawData({"fullName":"Lucian","address":{"city":"Compton","state":"Ca"}});

Take a look at this working fiddle i made: https://jsfiddle.net/1fjyrjn5/2/

Also some reference which helped me on this question:

Extjs 5, data model association & load nested data

https://www.sencha.com/forum/showthread.php?237602-Ext.data.association.HasOne-getting-started-example

Extjs hasone association