PDA

View Full Version : [1.1rc1] getNodeById() fails after replaceChild()


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 ) );

mystix
03-24-2008, 06:18 AM
you really shouldn't be using a release candidate version when the stable version is up.

have you tried your code with 1.1.1 final?


[edit]
on closer inspection, that's going to fail regardless...
you might want to use Ext.override instead:

Ext.override(Ext.tree.TreeNode, {
replaceChild: function( n, o ) {
var sib = null;
if (o)
{
sib = o.nextSibling;
this.removeChild( o );
}
this.insertBefore( n, sib );
}
});

then include that override in a separate overrides js file

avrndef
03-27-2008, 10:23 AM
The bug still occurs with 1.1.1. I would test with 2.0.2 too but I can't get it to work.