Question

I've doubt when we create custom event in flex.

Why do we use 'type:String, bubbles:Boolean=false, cancelable:Boolean=false' these parameter in flex events.

Was it helpful?

Solution

Bubbling causes a dispatched event to continue to be dispatched up the display tree until it reaches the stage. This is useful in various scenarios.

For example: Imagine you have several buttons inside a parent DisplayObject. You could add listeners to each button, and remember to remove them afterwards, or you could just add one listener to the parent. This works because MouseEvents have bubbling enabled.

buttonParent.addEventListener(MouseEvent.CLICK,handleButtonClick);

function handleButtonClick(event:MouseEvent):void
{
    trace("The button clicked was " + event.target.name);
}

The benefit of this is that you can now add and remove buttons freely, without having to worry about attaching listeners to them. The target property of the event object will be a reference to the button that was clicked, and currentTarget will be a reference to the parent.

Cancelable is a flag that sets whether or not you are permitted to stop the default action of an event by calling the preventDefault() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top