PDA

View Full Version : Ext.data.BeanXmlReader



onlysimchas
13 Feb 2008, 10:38 AM
This is a fairly simple XmlReader, but some of you might find it useful.

When I was creating a "Spring Java Bean " explorer, I came across a bug which prevents the word "class" being used when reading XML from a XHR response. This is annoying when the XML being read is java spring-like in nature, since often there are stanzas such as <bean class="com.x.y.z" /> and since it thinks they are className's and expects the Browser/DOM to properly convert the class attribute to valid css classes,, i could not find a way to get the "@class" to work, since it's hardcoded in the Ext.data.XmlReader..

So I created this Ext.data.BeanXmlReader (http://www.jsloader.com/twiki/pub/SpringThings/BeanCatalog/Ext.data.BeanXmlReader.js) which copies the "class" XML attribute to a provided attributename, so it can be made available by the superclass (XmlReader).

Here's the link to the page, (click the demo, etc to see it in use): http://www.jsloader.com/twiki/bin/view/SpringThings/

If you want the source, here it is:



Ext.data.BeanXmlReader = function(meta, recordType, newAttrName){
this.newAttributeName=newAttrName;
meta = meta || {};
Ext.data.BeanXmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
};
Ext.extend(Ext.data.BeanXmlReader, Ext.data.XmlReader, {

read : function(response){
this.copyClasses(response.responseXML);
return Ext.data.BeanXmlReader.superclass.read.call(this,response);
},

copyClasses:function(doc){
if(!doc) return;
var _beans=doc.getElementsByTagName("bean");
if(_beans || _beans.length){
for (var i=0;i<_beans.length;++i) {
_beans[i].setAttribute(this.newAttributeName,_beans[i].getAttribute("class"));
}
}
}
});


Usage:


beanReader=new Ext.data.BeanXmlReader({
record:'bean',
id: '@xclass'
}, [{name:"extends",mapping:"@extends"},{name:"implements",mapping:"@implements"},{name:"xclass",mapping:"@xclass"}],
"xclass" /* the name of the attribute to copy @class to */
);