2 May 2011
Beginning
In previous exercises you created an Employee Portal: Vehicle Request Form. The application UI (the form) is encapsulated in a custom component file and the main application manages the data retrieval and transforms the returned employee data into an ArrayCollection instance of employee value objects. This data is passed to the custom component, via a custom property, and then displayed in the DropDownList control.
Remember that a value object is a packet of related data that you want to treat as a unit. For example, in the case of an employee, the first name, last name, email address, and so on, constitute a unit of data. In the first part of this exercise, you will aggregate data from the form into a second packet of data, or value object, that contains all the vehicle request information.
In the second part of this exercise, you will create a custom event class to dispatch the vehicle request data from the custom component back to the main application. Keep in mind that a custom event class is required when you are dispatching custom data in an event object.
When the user clicks the Submit button for the form, the vehicle request value object is created, passed to the new custom event object and then dispatched to the main application. The user is presented with an alert message that the form has been submitted (see Figure 1).
In this section you will create a new ActionScript class to represent the Employee Portal: Vehicle Request form data.
You should see the VehicleRequest.as file in the Package Explorer view (see Figure 4). You should also see the VehicleRequest.as file open in the Editor view.
private variable named id data typed to the Object class. Modify this variable so that the access modifier is public and so it is data typed to the String class.
public class VehicleRequest
{
public var id:String;
public function VehicleRequest()
{
}
}
id variable, use the quick assist tool to create four more public variables named phone, mobilePhone, pickupDate, and returnDate. Data type the variables to the String class.public class VehicleRequest
{
public var id:String;
public var phone:String;
public var mobilePhone:String;
public var pickupDate:String;
public var returnDate:String;
public function VehicleRequest()
{
}
}
In this section you will use the click event of the Submit Request Button control to call a function that will assign property values to the VehicleRequest value object based on the information in the form fields.
FormItem container that nests the Button control.click event using the content assist tool (see Figure 5).
Script block, locate the submitButton_clickHandler() function.submitButton_clickHandler() function, delete the generated code stub, create a local variable named vehicleRequestData and use the content assist tool to data type it to the VehicleRequest class (see Figure 7).
import statement for the valueObjects.VehicleRequest class was created below the others.<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CalendarLayoutChangeEvent;
import valueObjects.VehicleRequest;
submitButton_clickHandler() function, set the vehicleRequestData instance to a new instance of the VehicleRequest class. The vehicleRequestData instance is a value object that you will now populate with data from the form.protected function submitButton_clickHandler(event:MouseEvent):void
{
var vehicleRequestData:VehicleRequest = new VehicleRequest();
}
vehicleRequestData variable declaration, set the vehicleRequestData.id value to the dropDownList.selectedItem.id value.
protected function submitButton_clickHandler(event:MouseEvent):void
{
var vehicleRequestData:VehicleRequest = new VehicleRequest();
vehicleRequestData.id = dropDownList.selectedItem.id;
}
vehicleRequestData.phone value to the phone TextInput control's text property value.protected function submitButton_clickHandler(event:MouseEvent):void
{
var vehicleRequestData:VehicleRequest = new VehicleRequest();
vehicleRequestData.id = dropDownList.selectedItem.id;
vehicleRequestData.phone = phone.text;
}
vehicleRequestData.mobilePhone value to the mobilePhone TextInput control's text property value.protected function submitButton_clickHandler(event:MouseEvent):void
{
var vehicleRequestData:VehicleRequest = new VehicleRequest();
vehicleRequestData.id = dropDownList.selectedItem.id;
vehicleRequestData.phone = phone.text;
vehicleRequestData.mobilePhone = mobilePhone.text;
}
vehicleRequestData.pickupDate value to the pickupDate.selectedDate.toDateString() value.Using the toDateString() method allows you to format the selected date as a String value so that it is compatible with the data on the server.
protected function submitButton_clickHandler(event:MouseEvent):void
{
var vehicleRequestData:VehicleRequest = new VehicleRequest();
vehicleRequestData.id = dropDownList.selectedItem.id;
vehicleRequestData.phone = phone.text;
vehicleRequestData.mobilePhone = mobilePhone.text;
vehicleRequestData.pickupDate = pickupDate.selectedDate.toDateString();
}
vehicleRequestData.returnDate value to the returnDate.selectedDate.toDateString() value.protected function submitButton_clickHandler(event:MouseEvent):void
{
var vehicleRequestData:VehicleRequest = new VehicleRequest();
vehicleRequestData.id = dropDownList.selectedItem.id;
vehicleRequestData.phone = phone.text;
vehicleRequestData.mobilePhone = mobilePhone.text;
vehicleRequestData.pickupDate = pickupDate.selectedDate.toDateString();
vehicleRequestData.returnDate = returnDate.selectedDate.toDateString();
}
In this section you will use the Flash Builder Debugger to view the data in the value object that you just created.
submitButton_clickHandler() function (see Figure 8).
You should see the application run in a browser.
You should see the Confirm Perspective Switch dialog box shown in Figure 9.
You should see the properties are populated with values from the Form container (see Figure 10).
You will dispatch the value object from the custom component to the main application. Remember that in order to pass custom data in an event, you must create a custom event class, which you will do in this section.
You should see the VehicleRequestEvent.as file in the Package Explorer view (see Figure 13). You should also see the VehicleRequestEvent.as file open in the Editor view.
public variable named vehicleRequestData and use the content assist tool to data type it to the VehicleRequest class (see Figure 14).
import statement for the valueObjects.VehicleRequest class has been created above the class definition.package events
{
import flash.events.Event;
import valueObjects.VehicleRequest;
public class VehicleRequestEvent extends Event
{
...
VehicleRequestEvent() function, remove the last two arguments.The type argument is used by the Event class to define the event type.
VehicleRequestEvent() function add the vehicleRequestData argument and data type it to the VehicleRequest class.With this code, you are declaring that this custom event class requires two arguments: the event type and the data from the form, packaged as a VehicleRequest value object.
public class VehicleRequestEvent extends Event
{
public var vehicleRequestData:VehicleRequest;
public function VehicleRequestEvent(type:String, vehicleRequestData:VehicleRequest)
{
super(type, bubbles, cancelable);
}
}
VehicleRequestEvent() function, from the super() method, remove the bubbles and cancelable arguments.public function VehicleRequestEvent(type:String,
vehicleRequestData:VehicleRequest){
super(type);
}
super() method, set the this.vehicleRequestData value to the vehicleRequestData object from the constructor's argument.public function VehicleRequestEvent(type:String,
vehicleRequestData:VehicleRequest){
super(type);
this.vehicleRequestData = vehicleRequestData;
}
In this section you will learn how to dispatch the custom event, with the form data, from the custom component to the main application and then handle the event by displaying an alert message to the user.
Metadata comment, create a Metadata block.<fx:Metadata>
</fx:Metadata>
Metadata block, type [Ev to invoke the content assist tool and declare the Event() method with the name argument set to vehicleRequestEvent and the type argument set to the events.VehicleRequestEvent class.<fx:Metadata>
[Event(name="vehicleRequestEvent", type="events.VehicleRequestEvent")]
</fx:Metadata>
import events.VehicleRequestEvent;
submitButton_clickHandler() function.submitButton_clickHandler() function, create a local variable named eventObject and data type it to the VehicleRequestEvent class.protected function submitButton_clickHandler(event:MouseEvent):void
{
...
vehicleRequestData.returnDate = returnDate.selectedDate.toDateString();
var eventObject:VehicleRequestEvent;
}
eventObject variable, assign a new instance of the VehicleRequestEvent class, passing vehicleRequestEvent as the type argument and the local vehicleRequestData variable value as the vehicleRequestData argument.protected function submitButton_clickHandler(event:MouseEvent):void
{
...
vehicleRequestData.returnDate = returnDate.selectedDate.toDateString();
var eventObject:VehicleRequestEvent = new VehicleRequestEvent("vehicleRequestEvent",vehicleRequestData);
}
eventObject variable declaration, use the dispatchEvent() method to dispatch the eventObject event.
protected function submitButton_clickHandler(event:MouseEvent):void
{
...
vehicleRequestData.returnDate = returnDate.selectedDate.toDateString();
var eventObject:VehicleRequestEvent = new VehicleRequestEvent("vehicleRequestEvent",vehicleRequestData); dispatchEvent(eventObject);
}
components:VehicleRequestForm instance.vehicleRequestEvent event (see Figure 15).
vehicleRequestEvent (see Figure 16). Note that Flash Builder automatically gave the component an instance name of vehiclerequestform1.
Script block, locate the vehiclerequestform1_vehicleRequestEventHandler() function.Alert.show() method to display the message, Your vehicle request has been submitted.protected function vehiclerequestform1_vehicleRequestEventHandler(event:Event):void
{
Alert.show("Your vehicle request has been submitted.");
}
You should see the Alert window displaying the message, "Your vehicle request has been submitted." (see Figure 17).
In this tutorial you consolidated your knowledge about creating custom events and value objects by creating a custom event that dispatched a value object, representing data from a form. You also handled the custom event in the main application and displayed an alert dialog to the user.
In the next videos and exercises, you will learn how to pass this data back to the server for insertion into the database.
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.