Triggering a change event in a Flex tree control programmatically

actionscript-3apache-flextree

I have a method to add an XML node structure to the currently selected tree node.

This appends the xml, and opens the parent node to display the newly added node.

I then select the node by setting the selectedItem of the tree.

I have an editing form that updates its values on the tree change event. When I set the selectedItem in this method, The node is selected correctly but the change event never fires (thus the editor doesnt update). I have tried to call it in a call later block to no avail.

Is there a way I can force the tree to dispatch a change event at this point?

public function addSelected(node:XML):void{

            tree_expandItem(false);             

            var selectedItem:XML = tree.selectedItem as XML;

            selectedItem.appendChild(node);

            tree_expandItem(true);

            callLater(function():void { tree.selectedItem = node; } );  

        }

To extend this question in a general sort of way – I would have thought that changing the selectedItem of the tree would result in a change event anyway? Or is a change only considered a change if the user makes it?

Best Answer

You could move the logic that is currently in your change event handler to a separate function, and then call that function directly:

private function changeHandler(event:ListEvent):void
{
    doChangeLogic();
}

private function doChangeLogic():void
{
    //statements
}

public function addSelected(node:XML):void
{
    tree_expandItem(false);                         

    var selectedItem:XML = tree.selectedItem as XML;

    selectedItem.appendChild(node);

    tree_expandItem(true);

    callLater(function():void { tree.selectedItem = node; } );

    doChangeLogic();
}