2 May 2011
Exercise 1.1: Setting up Flash Builder and your project files
Beginning
In this exercise you will use the application you made in Exercise 1.6 (Creating MXML custom components with ActionScript properties) to create an ActionScript class and use instances of the class to populate employee data (see in Figure 1).
In this exercise you will learn how to:
In this section you will create an ActionScript class.
private to public and change the data type to the String class by using the content assist tool (CTRL+Space).public class Employee
{
public var imageFile:String;
...
firstName and lastName.public class Employee
{
public var imageFile:String;
public var firstName:String;
public var lastName:String;
...
fileName, fName, and lName. Type all three parameters to the String class.public function Employee(fileName:String, fName:String,
lName:String)
{
}
public function Employee(fileName:String, fName:String,
lName:String)
{
imageFile = fileName;
firstName = fName;
lastName = lName;
}
Note: When the argument names in the constructor match the class property names, it is a best practice to add this to the constructor argument name so that you can differentiate between the constructor arguments and the class property names. In this case, the argument names and the class property names are different, so you do not need to add this to the constructor argument names.
In this section, you will create multiple employee instances with ActionScript.
Script block.<!-- Script
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Script>
<![CDATA[
]]>
</fx:Script>
Script block, type firstEmployee and use the quick assist tool (CTRL+1) to create a private variable and use the content assist tool (CTRL+Space) to data type the variable to the Employee class (see Figure 3).
Script block, ensure the components.Employee package was imported. If not, add the following code to import the package:<fx:Script>
<![CDATA[
import components.Employee;
private var firstEmployee:Employee
]]>
</fx:Script>
firstEmployee variable to a new instance of the Employee class component:private var firstEmployee:Employee =new Employee;
fileName parameter of aparker.jpg, a fName parameter of Athena and an lName parameter of Parker. private var firstEmployee:Employee = new Employee
("aparker.jpg","Athena","Parker");
private variable named secondEmployee, data typed to the Employee class.private var firstEmployee:Employee = new Employee
("aparker.jpg","Athena","Parker");
private var secondEmployee:Employee
secondEmployee variable to a new instance of the Employee class component, passing a fileName parameter of stucker.jpg, a fName parameter of Saul and anlName parameter of Tucker.private var secondEmployee:Employee = new Employee
("stucker.jpg","Saul","Tucker");
EmployeeDisplay component instance.firstEmployee.imageFile to the first component's imageFile property as a bindable value:<components:EmployeeDisplay x="10" y="60"
imageFile="{firstEmployee.imageFile}"
fullName="Athena Parker"/>
You should see two binding warnings in the Problems view (see Figure 4).
firstEmployee and secondEmployee variables bindable by using content assist to add the Bindable keyword in brackets:[Bindable]
private var firstEmployee:Employee = new Employee
("aparker.jpg","Athena","Parker");
[Bindable]
private var secondEmployee:Employee = new Employee
("stucker.jpg","Saul","Tucker");
The binding warning still exists for the imageFile variable (see Figure 5).
imageFile variable to invoke the content assist tool. Add the Bindable declaration to the imageFile variable.[Bindable]
public var imageFile:String;
Note that the warnings no longer exists.
imageFile property, add the secondEmployee.imageFile bindable value:<components:EmployeeDisplay x="105" y="60"
imageFile="{secondEmployee.imageFile}"
fullName="Saul Tucker"/>
In this section you will bind the components to the employee instances.
Script block, locate and delete the two bindable variables.import statements comment, import the Employee class component.// import statements ----------------------------------------
import components.Employee;
Bindable public variable named employeeData and assign the variable a data type of the Employee class.// variable declarations ------------------------------------
[Bindable]
public var employeeData:Employee;
BitmapImage control tag.source property's binding to display the info from the employeeData variable:<s:BitmapImage source="images/{employeeData.imageFile}" />
Label control and delete the value of the text property.<s:Label x="0" y="80" text=""/>
Note: You will see four errors populate the Problems view. You will fix these next.
component tag, remove the imageFile and fullName properties:<components:EmployeeDisplay x="10" y="60"/>
employeeData property and bind it to the value of the firstEmployee variable:<components:EmployeeDisplay x="10" y="41"
employeeData="{firstEmployee}"/>
<components:EmployeeDisplay x="105" y="60"
employeeData="{secondEmployee}"/>
Your application should appear as shown in Figure 6.
In this section you will create a class method to display an employee's names.
Employee() method, create a new method named createFullName that takes no parameters and returns data typed to the String class:...
lastName = lName;
}
public function createFullName():String
{
}
firstName variable and the lastName variable with a space between them:public function createFullName():String
{
return firstName + " " + lastName;
}
In this section you reuse the createFullName() function to dynamically display the employee name below the BitmapImage control.
text property to the employeeData variable evaluated by the createFullName() function:<s:Label x="10" y="92"
text="{employeeData.createFullName()}"/>
The components now display the employee's names (see Figure 7).
In this exrecise you learned how to create an ActionScript class and use instances of it to display data. In the next exercise you will convert this ArrayCollection of generic objects into an ArrayCollection of typed data.
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.