Creating Another Value Object


In this task, you will build a simple value object. Up to this point, you have retrieved only product information; but, as the application continues to grow, it will also be important to retrieve information about the categories in which the products are grouped. For this reason, you need a Category value object that will hold the category name and a category ID, which is the primary key field for the category.

1.

Right-click the valueObjects folder. Choose New > ActionScript Class. In the New ActionScript Class dialog box, be sure that the Package Name is valueObjects and supply the Name of the new class to be Category. Also, be sure that the only modifier checked is public; then click Finish. You should see the code automatically generated for the class appears as follows:

package valueObjects {    public class Category    {    } } 


This creates the skeleton for the new Category value object class.

Note

The positioning of opening braces is purely a style issue. Some developers like the opening brace at the end of the preceding line of code. For instance, the skeleton class code generated by Flex Builder is just as valid with the open braces positioned at the end of the preceding lines of code:

package valueObjects{    public class Category{    } } 


2.

Make the class bindable because the two properties in the class both need to be bindable. In the class, create two public properties: the first is catID, data typed as int; the second is catName, data typed as String.

package valueObjects{    [Bindable]    public class Category{       public var catID:int;       public var catName:String;    } } 


When dealing with the categories, there are obviously times when you want to see the category name; hence the need for the catName property. You will also be retrieving products based on category, and that will be done by category ID; hence the reason for the catID property.

3.

Below the variable declarations, build the constructor function so it accepts two parameters: the first is id, data typed as int; the second is catName, data typed as String. In the constructor function, assign the catID property the value of the parameter id and the catName property the value of the parameter name.

public function Category(id:int,catName:String){    this.catID=id;    this.catName=catName; } 


When the property names match the parameter names, the class' properties must be preceded by the this prefix when assigning the values in the constructor. So, in this case, only the catName property had to use the this prefix because the name of the property and the name of the parameter are the same.

4.

Create a public toString() function data typed as String. In the function, return the word Category in square brackets, followed by a space, and then the category name.

public function toString():String{    return "[Category] "+ catName; } 


It is always a good idea to create a toString() function for a value object in case the object is ever used in a trace statement.

5.

Check to be sure your completed Category value object appears as follows:

package valueObjects{    [Bindable]    public class Category{       public var catID:int;       public var catName:String;       public function Category(id:int,catName:String){          this.catID=id;          this.catName=catName;       }       public function toString():String{          return "[Category] "+ catName;       }    } } 


With the category value object now created, you can begin work on the nonvisual data manager component.




Adobe Flex 2.Training from the Source
Adobe Flex 2: Training from the Source
ISBN: 032142316X
EAN: 2147483647
Year: 2006
Pages: 225

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net