PDA

View Full Version : XMLDataModel.createNode revision: safe?



CurtF
1 Feb 2007, 9:16 AM
Since I needed to have grids that start with XML from a webservice, but may have no items, and will grow/shrink to the user's whim; I needed to work around the "clone" approach that the current createNode uses. I'd rather not create a specific impl for each time I use the grid.

I've only just started looking at your very nice package and so far really enjoy and appreciate it's simplicity.

I'm posting my revision here - mind glancing at it and commenting on if it is safe and a correct approach? tnx.


YAHOO.ext.grid.XMLDataModel.prototype.createNode = function(xmlDoc,id,colData)
{
var template;
var newNode;

//-Changed
if( this.data.length == 0 )
{
newNode = document.createElementNS( xmlDoc.namespaceURI, this.schema.tagName );
for( var i=0, len=this.schema.fields.length; i<len;i++) {
newNode.appendChild( document.createElement( this.schema.fields[i] ) );
}
}
else {
template = this.data[0].node;
newNode=template.cloneNode(true);
}
//-end changed

var fields=this.schema.fields;
for(var i=0,len=fields.length;i<len;i++)
{
var nodeValue=colData[i];
if(this.postprocessors[i])
{
nodeValue=this.postprocessors[i](nodeValue);
}
this.setNamedValue(newNode,fields[i],nodeValue);
}

if(id)
{
this.setNamedValue(newNode,this.schema.idField,id);
}

//-Changed
if( this.data.length == 0 )
xmlDoc.appendChild(newNode);
else
template.parentNode.appendChild(newNode);
//-end changed
return newNode;
};