PDA

View Full Version : Mapping XML attributes from child elements



Eraesr
3 Apr 2012, 6:21 AM
Hi,

I've got some trouble mapping some attribute data in XML elements I'm loading into my store. I found this thread (http://www.sencha.com/forum/showthread.php?191330-XML-mapping-for-attribute-always-returns-empty) which kind of describes the same problem, but I am looking for mapping an attribute from a child element.

So my XML looks like this:

<entries>
<entry>
<id name="capNews" />
<description>Caption for news section</description>
<value>News</value>
</entry>
</entries>


The model looks like this:


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


config: {
fields: [
{ name: 'name', mapping: 'id@name', type: 'string' },
{ name: 'value', type: 'string' }
]
}
});


The store/reader/proxy is set up as follows:


var reader = new Ext.data.XmlReader({ record: 'entry' });

var proxy = Ext.create('Ext.data.proxy.Ajax', {
url: wordsurl,
model: 'TSW.model.nomenclature',
reader: reader
});
var store = storeManager.getNomenclatureStore();
store.setProxy(proxy);

store.load(....)


Now the value field is loaded properly into the models, but the name field turns up as null. I've tried various mappings like 'id[name]', '@name', 'id/name' but so far I haven't been able to get this to work. Does anyone know what I'm doing wrong?

Oh, to test the contents of the store I'm doing this:


for (var i = 0; i < store.getCount(); i++) {
console.log(store.getAt(i).get('name') + ' => ' + store.getAt(i).get('value'));
}

bandsitebuilder
3 Apr 2012, 7:10 AM
name > @id

Eraesr
3 Apr 2012, 11:03 PM
Thanks for the reply, but it doesn't appear to work. Maybe I am misunderstanding you? I have this now:



fields: [
{ name: 'name', mapping: 'name > @id', type: 'string' },

{ name: 'value', type: 'string' }

]

bandsitebuilder
4 Apr 2012, 8:11 AM
my bad.. I misread your XML schema

your mapping should be id > @name

BUT I'm new to this and I'm not sure if your attribute "name" isn't going to conflict somehow with any reserved javascript/sencha naming protocols.. if it doesn't work, try renaming the attribute to something like "theName" or similar

Eraesr
6 Apr 2012, 6:20 AM
Hi,

It still doesn't seem to work. I tried renaming all instances of "name" (so the name attribute, the reference to the attribute and the fieldname in the model to "aname" instead, to prevent any cases of keyword conflicts like you said, but that doesn't seem to help either. I'm not sure what I'm doing wrong :s

Eraesr
23 Apr 2012, 4:38 AM
Ok, I solved it with a bit of a hacky approach. It may not be the best (or preferred) solution, but it works.
My model looks like this now. The 'name' attribute has a convert property that refers to a getName function.



Ext.define('MyApp.model.nomenclature', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'name', convert: getName, type: 'string' },
{ name: 'value', type: 'string' }
]
}
});


I also have the function getName defined as follows:


function getName(v, record) {
var nodes = record.raw.childNodes;


for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (node.nodeName == 'id') {
var attributes = node.attributes;
for (var a = 0; a < attributes.length; a++) {
var attribute = attributes[a];
if (attribute.name == 'name')
return attribute.value;
}
}
}


return null;
}


It basically just drills down into the XML structure of the record as it was retrieved from the server and returns the value of the name attribute of the node.

Nicext
22 Jun 2012, 2:23 AM
Sorry, but I'm having the same problem in Sencha touch 2.
Ok for the convert function. It works.
But it's a workaround. It must work with mapping: 'id > @name'
So what is wrong?