⬆️ ⬇️

Self-Destroying EventLisener in Flex / AS

It so happened that I was working on a Flex project ...



Task:

Make a "one-time" EventListener.



Problem:

How? :)



Solution (spherical example in vacuum):

')

MXML part:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">

<!-- -->

<mx:Button id="button1" />

</mx:Application>



AS-part:

private function init():void{

var onClick: Function = function ( event :MouseEvent):void{

trace( "event handled" );

button1.removeEventListener(MouseEvent.CLICK, onClick);

}

button1.addEventListener(MouseEvent.CLICK, onClick);

}




* This source code was highlighted with Source Code Highlighter .

private function init():void{

var onClick: Function = function ( event :MouseEvent):void{

trace( "event handled" );

button1.removeEventListener(MouseEvent.CLICK, onClick);

}

button1.addEventListener(MouseEvent.CLICK, onClick);

}




* This source code was highlighted with Source Code Highlighter .

private function init():void{

var onClick: Function = function ( event :MouseEvent):void{

trace( "event handled" );

button1.removeEventListener(MouseEvent.CLICK, onClick);

}

button1.addEventListener(MouseEvent.CLICK, onClick);

}




* This source code was highlighted with Source Code Highlighter .




As a result, the button is clicked once. Hooray!



Source: https://habr.com/ru/post/58080/



All Articles