We discovered a couple of very strange bugs in how Flex trees handle data binding today. Our tree has showRoot=”false”. We are using e4x to bind to a sub-element of some source XML. Here is a sample MXML file:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”vertical” creationComplete=”init()”>
<mx:Script>
<![CDATA[
public function onAdd():void
{
x2.structure.appendChild(<entry label=”dynamic”/>);
}
]]>
</mx:Script>
<mx:XML id=”x1″ xmlns=””>
<project label=”root”>
<metadata label=”metadata root”>
<entry label=”1st metadata entry”/>
<entry label=”2nd metadata entry”/>
</metadata>
<structure label=”content”>
<entry label=”one”>
<entry label=”one.one”/>
</entry>
<entry label=”two”/>
</structure>
</project>
</mx:XML>
<mx:XML id=”x2″ xmlns=””>
<project label=”root”>
<metadata label=”metadata root”>
<entry label=”1st metadata entry”/>
<entry label=”2nd metadata entry”/>
</metadata>
<structure label=”structure”>
</structure>
</project>
</mx:XML><mx:Tree id=”t1″ dataProvider=”{x1.structure}” showRoot=”false” labelField=”@label” width=”100″ height=”100″/>
<mx:Tree id=”t2″ dataProvider=”{x2.structure}” showRoot=”false” labelField=”@label” width=”100″ height=”100″/>
<mx:Button label=”Add” click=”onAdd()”/></mx:Application>
The first thing you notice when you run this is that while the second tree (on the left) should be empty it is in fact not! The showRoot=”false” setting does not appear to be honored if the root element doesn’t have any children. This appears to be written up at Adobe as Bug SDK-15083.
The second extremly strange behavior occurs when we add an item to the structure container that’s mapped to the second tree (we do this by clicking the Add button). Instead of ending up with a tree with our new entry, we end up with a tree that has a node for the metadata element and a node for the structure element… It appears that the databinding has been pushed up a level somehow (see picture on right.)
We were able to workaround the first bug using the workaround described in the SDK-15083 writeup. We had to force a full refresh of the data binding for the tree when adding a first item in order to resolve the second issue.
How did you force the full refresh of the tree? Calling its invalidateList() method followed by its validateNow() method?
Andrew, sorry, don’t remember, we simply followed some instructions in the bug writeup.
Hi binduwavell,
There is no workaround and there are no instructions attached to the bug entry you mentioned… ( http://bugs.adobe.com/jira/browse/SDK-15083 ) – can you remember where the workaround was? I think it’d be very helpful for many people. (I am, clearly, in a similar situation)
Daniel
We simply called tree.dataSource = to refresh the data binding. For the first issue, we implemented our own data descriptor as described in the bug.