Macromedia flex 2-migrating applications to flex 2 Manual De Usuario
Using function listeners
115
To find the appropriate static constant for your event type, see the events section of the
control’s entry in Adobe Flex 2 Language Reference.
control’s entry in Adobe Flex 2 Language Reference.
Using function listeners
When migrating, convert all object event listeners to function event listeners. You can no
longer pass an object as the second parameter to the
longer pass an object as the second parameter to the
addEventListener()
method. The
listener argument of the
addEventListener()
method was of type Object, which also
accepted a Function, but is now of type Function. If you try to pass an object listener, Flex
reports an error.
reports an error.
For example, if you had the following:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
creationComplete="createHandler()">
<mx:Script>
import mx.core.Alert;
function createHandler() {
import mx.core.Alert;
function createHandler() {
var myListener = new Object();
myListener.click = function(event) {
myListener.click = function(event) {
Alert.show("This is a log message");
}
myButton.addEventListener("click", myListener);
trace("Added listener");
myButton.addEventListener("click", myListener);
trace("Added listener");
}
</mx:Script>
<mx:Button label="Click Me" id="myButton"/>
</mx:Script>
<mx:Button label="Click Me" id="myButton"/>
</mx:Application>
Convert it to the following:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="createHandler()">
<mx:Script>
import mx.controls.Alert;
private function createHandler():void {
import mx.controls.Alert;
private function createHandler():void {
trace("Added listener");
button1.addEventListener(MouseEvent.CLICK, myClickHandler);
button1.addEventListener(MouseEvent.CLICK, myClickHandler);
}
private function myClickHandler(event:MouseEvent):void {
private function myClickHandler(event:MouseEvent):void {
Alert.show("This is a log message");
}
</mx:Script>
<mx:Button label="Click Me" id="button1"/>
</mx:Script>
<mx:Button label="Click Me" id="button1"/>
</mx:Application>