avrndef
03-24-2008, 06:15 AM
If, on a TreeNode, you
(1) do a replaceChild() with the new child having the same ID as the old child
and
(2) do a getNodeById() with the id,
you will get an undefined.
This is because TreePanel keeps an ID hash. The default implementation of replaceChild inserts the new node and then removes the old node. This means that the ID gets added into the hash (while it already exists) and then deleted.
I have code to demonstrate and fix the issue.
var tree = new Ext.tree.TreePanel('tree-div', {});
var root = new Ext.tree.AsyncTreeNode( { id: 1, text: 'root' } );
tree.setRootNode( root );
var child = new Ext.tree.AsyncTreeNode( { id: 2, text: 'bla' } );
root.appendChild( child )
alert( "(1) Initial: " + tree.getNodeById( 2 ) );
root.replaceChild( new Ext.tree.AsyncTreeNode( { id: 2, text: 'xorf' } ), child );
alert( "(1) Replaced: " + tree.getNodeById( 2 ) );
Ext.tree.TreeNode.prototype.replaceChild = function( n, o ) {
var sib = null;
if (o)
{
sib = o.nextSibling;
this.removeChild( o );
}
this.insertBefore( n, sib );
}
var root = new Ext.tree.AsyncTreeNode( { id: 1, text: 'root' } );
tree.setRootNode( root );
var child = new Ext.tree.AsyncTreeNode( { id: 2, text: 'bla' } );
root.appendChild( child );
alert( "(2) Initial: " + tree.getNodeById( 2 ) );
root.replaceChild( new Ext.tree.AsyncTreeNode( { id: 2, text: 'xorf' } ), child );
alert( "(2) Replaced: " + tree.getNodeById( 2 ) );
(1) do a replaceChild() with the new child having the same ID as the old child
and
(2) do a getNodeById() with the id,
you will get an undefined.
This is because TreePanel keeps an ID hash. The default implementation of replaceChild inserts the new node and then removes the old node. This means that the ID gets added into the hash (while it already exists) and then deleted.
I have code to demonstrate and fix the issue.
var tree = new Ext.tree.TreePanel('tree-div', {});
var root = new Ext.tree.AsyncTreeNode( { id: 1, text: 'root' } );
tree.setRootNode( root );
var child = new Ext.tree.AsyncTreeNode( { id: 2, text: 'bla' } );
root.appendChild( child )
alert( "(1) Initial: " + tree.getNodeById( 2 ) );
root.replaceChild( new Ext.tree.AsyncTreeNode( { id: 2, text: 'xorf' } ), child );
alert( "(1) Replaced: " + tree.getNodeById( 2 ) );
Ext.tree.TreeNode.prototype.replaceChild = function( n, o ) {
var sib = null;
if (o)
{
sib = o.nextSibling;
this.removeChild( o );
}
this.insertBefore( n, sib );
}
var root = new Ext.tree.AsyncTreeNode( { id: 1, text: 'root' } );
tree.setRootNode( root );
var child = new Ext.tree.AsyncTreeNode( { id: 2, text: 'bla' } );
root.appendChild( child );
alert( "(2) Initial: " + tree.getNodeById( 2 ) );
root.replaceChild( new Ext.tree.AsyncTreeNode( { id: 2, text: 'xorf' } ), child );
alert( "(2) Replaced: " + tree.getNodeById( 2 ) );