2 May 2010
Beginning
In this exercise you will use the addEventListener() method to handle a change in the date selected from a DateChooser control (see Figure 1).
In this exercise you will learn how to:
addEventListener() method to handle a change eventIn this section you will change the sample application to use the addEventListener() method to assign an event handler.
employeeService.send() method out of the creationComplete event and call a new function named initApp():<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="850"
creationComplete="initApp()">
Script block.Script block, below the dateChangeHandler() function, create a private function named initApp() that takes no parameters and returns void.private function initApp():void
{
}
initApp() function, paste the employeeService.send() method.private function initApp():void
{
employeeService.send();
}
addEventListener() method to call the dateChangeHandler()function upon a change event to the pickupDate DateChooser control.Note: Remember to use the event constant CalendarLayoutChangeEvent.CHANGE rather than the change string for the event type.
private function initApp():void
{
employeeService.send();
pickupDate.addEventListener(CalendarLayoutChangeEvent.CHANGE,_dateChangeHandler);
}
pickupDate.addEventListener(...) line and paste it below the existing line.returnDate:private function initApp():void
{
employeeService.send();
pickupDate.addEventListener(CalendarLayoutChangeEvent.CHANGE,_dateChangeHandler);
returnDate.addEventListener(CalendarLayoutChangeEvent.CHANGE,_dateChangeHandler);
}
You should see the Alert message displayed twice, since the DateChooser instances still have defined change events (see Figure 2).
If the return date is before the pickup date, you will see the image in Figure 3. If it is after the pickup date, your Alert message will appear, similar to Figure 2. You should also see the Alert message display a message according to the date selected. The Alert messages will appear twice because the DateChooser controls still have events defined.
<mx:DateChooser id="pickupDate"/>
<mx:DateChooser id="returnDate"/>
You should see that the Alert messages only appear once as expected.
In this exercise you learned how to use the addEventListener() method to handle a change in a DateChooser control. In the next exercise you will learn how to use the HTTPService object's result event to store data in a variable that is bound to Flex controls.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.