PDA

View Full Version : How to define relations between different model object



flabs
19 Aug 2013, 6:30 PM
I need some help how to define relation between two model Objects.

I have the following two model Objects EthernetPort and EthernetInterface

EthernetInterface will have a list of EthernetPort Objects. Not sure how to define this relation. I am able to generate the json string from server with EthernetPorts as an array.


Ext.define('EthernetPorts', {
extend: 'Ext.data.Model',
fields: [{name:'name', type:'string'},{name:'id',type:'string'}]
});

Ext.define('EthernetInterface', {
extend: 'Ext.data.Model',

fields: [
{name:'id',type:'string'},
{ name: 'name', type: 'string'},
{ name: 'auto_negotiation', type: 'string'},
{ name: 'redundancy', type: 'string'},
{name:'speed', type:'string'},
{name:'duplex', type:'string'},
{name:'max_bandwidth', type:'string'},
{name:'ethport_list', type:'string'} This needs to be list of Port model object
]
});

Renan Cunha
19 Aug 2013, 6:43 PM
Hello,
You dont treat the EthernetPort list as an field (property) of your EthernetInterface.
You need to configure an 'association' in your parent model (the eth interface).

Your ethernet interface model will be something like that:



Ext.define('EthernetInterface', {
extend: 'Ext.data.Model',


fields: [
{ name: 'id',type:'string'},
{ name: 'name', type: 'string'},
{ name: 'auto_negotiation', type: 'string'},
{ name: 'redundancy', type: 'string'},
{ name: 'speed', type:'string'},
{ name: 'duplex', type:'string'},
{ name: 'max_bandwidth', type:'string'}
],


associations: [{
type: 'hasMany',
foreignKey: 'ethinterface_id', //it would be the fk key on the eth port relationed with eth interface
model: 'EthernetPort',
associationKey: 'ethport_list'
}]
});

flabs
19 Aug 2013, 7:09 PM
Thank you for your reply, the json that is generated from server is follows:

[{"id":1,"name":"test1233","redundancy":"enabled","autoNegotiation":"Disabled","speed":"1000","duplex":"full","maxBandwidth":"1000","ver":1,
"ethernetPorts":[{"id":1,"name":"eth0","usage":"Management"},{"id":2,"name":"eth1","usage":"Management"}]}

I want show this data in a form to modify, with ports as a single column grid, which user can add or delete the ports from the list.

Do i need to create two stores are one store sufficient to update the Port list.

Thanks much for your help