Flex pass event handler and add it to dynamically created object inside itemrenderer
I have a Tree
, which has an itemRenderer
. On the itemRenderer
, driven by the XML conditions, objects are created dynamically and added as a children to nodes.
These new objects shall trigger an event handler ( some click events mostly ).
- How do I pass the event handler to the tree and later to th开发者_运维问答e dynamically created new objects inside
itemRendere
?
Something like:
<mx:Tree x="534" y="49" newObjectsOnClick="newObjectsOnClickHandler">
Use event bubbling for events dispatched from item renderers.
So create a custom event as the following:
public class MyEvent extends Event
{
public static const SOME_ACTION_PERFORMED:String = "someActionPerformed";
public function MyEvent(type:String)
{
// The second parameter is for bubbling!
super(type, true, false);
}
override public function clone():Event
{
return new MyEvent(type);
}
}
}
Then in renderer:
dispatchEvent(new MyEvent(MyEvent.SOME_ACTION_PERFORMED));
And in class which contains your Tree
:
private function onInit():void
{
addEventListener(MyEvent.SOME_ACTION_PERFORMED, someActionHandler);
}
private function someActionHandler(event:MyEvent):void
{
// Perform necessary actions here
…
// Then stop bubbling
event.stopImmediatePropagation();
}
精彩评论