PDA

View Full Version : Ext.data.Model : Associations with XML datas



swissdotnet
27 Sep 2011, 5:34 AM
Hello,

I'm loading a store from a xml file with a Ext.data.Model.
It's working very well except for the association part, I dont get any datas from the association when I use the created method "receivers()" from an operator record.

What am I doing wrong please ? :(

My XML :

<operators>
<operator name="Swissdotnet" status="up" id="1">
<receivers>
<receiver name="Gateway1-p1" status="up" id="2"/>
<receiver name="Gateway1-p2" status="up" id="3"/>
<receiver name="Gateway2-p1" status="up" id="4"/>
<receiver name="Gateway2-p2" status="up" id="5"/>
</receivers>
</operator>
<operator name="Swissdotnet" status="up" id="6">
<receivers>
<receiver name="Gateway1-p1" status="up" id="7"/>
<receiver name="Gateway1-p2" status="up" id="8"/>
<receiver name="Gateway2-p1" status="up" id="9"/>
<receiver name="Gateway2-p2" status="up" id="10"/>
</receivers>
</operator>
</operators>

My operator data model :

Ext.define('Operator', {
extend: 'Ext.data.Model',
fields: [{
name : 'id',
mapping :'@id'
},{
name : 'name',
mapping :'@name'
},{
name : 'status',
mapping :'@status'
}],
hasMany: [{model: 'Receiver', name: 'receivers'}]
});

My receiver data model :

Ext.define('Receiver', {
extend: 'Ext.data.Model',
fields: [{
name : 'id',
mapping :'@id'
},{
name : 'name',
mapping :'@name'
},{
name : 'status',
mapping :'@status'
}],
belongsTo : 'Operator'
});

My store :

this.operatorsStore = Ext.create('Ext.data.Store', {
model: 'Operator',
proxy: {
type: 'ajax',
url: transmitterDataURL,
reader: {
type: 'xml',
record: 'operator'
}
}
});

piziwate
27 Sep 2011, 10:51 PM
Hello swissdotnet !

I got the same problem last week ! I still didn't foud any answer ! :s

Any idea Ext Dev Team ??

Best regards

Pizi

skirtle
28 Sep 2011, 2:24 AM
There may be another way but...

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.HasManyAssociation-cfg-reader


hasMany: [{model: 'Receiver', name: 'receivers', reader: {type: 'xml', record: 'receiver'}}]

swissdotnet
28 Sep 2011, 3:34 AM
It's working perfectly, thank you !

skirtle
28 Sep 2011, 3:43 AM
For future reference, the way I found that was by reading the ExtJS source code. I just browsed the source of XML reader until I found the bit that handled associations. A quick play with a breakpoint later and it became obvious what setting was required to get it working. Then I checked the docs and found the setting was there.