2 May 2011
Beginning
In this exercise you will use the event object to create a reusable event handler function. You will also use a conditional statement to validate pickup and return dates (see Figure 1).
In this exercise you will learn how to:
In this section you will dispatch an event based on a change to the DateChooser control. More specifically, you will pass an event object from a DateChooser control to the event handler function.
Script block, locate the dateChangeHandler() function.dateChangeHandler() function, accept a parameter named event data typed to the CalendarLayoutChangeEvent class:private function dateChangeHandler(event:CalendarLayoutChangeEvent):void
{
Alert.show('You have selected ' +
pickupDate.selectedDate.toDateString());
}
import mx.events.CalendarLayoutChangeEvent;
id property of pickupDate.change property's value, pass a parameter named event.<mx:DateChooser id="pickupDate"
change="dateChangeHandler(event)"/>
returnDate DateChooser control.<mx:DateChooser id="returnDate"
change="dateChangeHandler(event)" />
In this section you will use the Flash Builder Debug perspective to explore how the event object works.
dateChangeHandler() function to add a breakpoint (see Figure 2).
This opens the Confirm Perspective window (see Figure 4).
type and target.id (see Figure 5).
In this section you will make the dateChangeHandler() function work for both the pickupDate and the returnDate DateChooser controls.
dateChangeHandler() function.pickupDate.selectedDate.toDateString() function, add the event.target variable in place of the id of the DataChooser control:private function
dateChangeHandler(event:CalendarLayoutChangeEvent):void
{
Alert.show('You have selected ' + event.target.selectedDate.toDateString())
}
Note: If you type event.target.selectedDate, Flash Builder will not automatically give you the toDateString() code hinting. This happens because Flash Builder does not know that this value is a Date instance. You can cast event.target.selectedDate as a Date instance by adding parentheses. Then when you type a period, the toDateSting() method will appear with content assist. Your code should read ((event.target.selectedDate) as Date).toDateString(). This will not change the functionality of the code.
pickupDate DateChooser control.You should see the Alert window display the date you selected.
returnDate DateChooser control. You should see the Alert window display the date you selected.
In this section you will make the application validate whether the return date request falls after the pickup date.
dateChangeHandler() function, below the Alert.show() method, press CTRL+Space twice to invoke the content assist tool's code templates and type if. Select the if statement template from the list of suggestions.returnDate instance and whether the date selected from the pickupDate DateChooser control is less than the date selected from the returnDate DateChooser control.Note: Use the equality operator (==) to test whether event.target.id is equal to the string returnDate.
private function dateChangeHandler(event:CalendarLayoutChangeEvent):void
{
Alert.show('You have selected ' +
event.target.selectedDate.toDateString());
if ((event.target.id == "returnDate") &&
(pickupDate.selectedDate > returnDate.selectedDate))
{
}
}
Alert.show() method to display the message, "Pickup date must be scheduled before return date." if ((event.target.id == "returnDate") &&
(pickupDate.selectedDate > returnDate.selectedDate))
{
Alert.show("Pickup date must be scheduled before return date.");
}
pickupDate DateChooser.You should see the Alert message display the selected date (see Figure 7).
returnDate DateChooser control that is before the date you selected for the pickupDate DateChooser control.You should see the Alert window with the message: Pickup date must be scheduled before return date (see Figure 8).
Note: Although this alert message is lower in the Script block than the first Alert message, it displays first because Flash layers alert message on top of each other.
dateChangeHandler() function, add another if statement below the first conditional statement, using the code template.pickupDate instance and whether the date selected from the pickupDate DateChooser control is less than the date selected from the returnDate DateChooser control.Alert.show() method to display the message, Pickup date must be scheduled before return date.if((event.target.id == “pickupDate”) &&
(pickupDate.selectedDate > returnDate.selectedDate) &&
(returnDate.selectedDate != null))
{
Alert.show(“Pickup date must be scheduled before return date.”)
}
returnDate DateChooser control.pickupDate DateChooser control that is after the date you selected in the returnDate DateChooser control.In this exercise you learned how to use the event object to handle events for multiple controls. In the next exercise you will use the addEventListener() method to handle a change in the date selected from a DateChooser control.
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.