Reading Data from an Existing Shared Object


In this task, you will read the information from the existing shared object and use this information to populate the DataGrid control that is displaying the information.

1.

Open views/ecomm/Cart.mxml.

You will populate the DataGrid, located in Cart.mxml, from the shared object that has already been written to the client machine.

2.

At the top of Cart.mxml, locate the <VBox> root tag and add a creationComplete event that will call the loadCart() method from the cart object.

<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"    creationComplete="cart.loadCart()"> 


It makes sense to call the method that will populate the DataGrid on the creationComplete event to be sure that the DataGrid is available for use.

3.

Save Cart.mxml and open valueObjects/ShoppingCart.as.

You will write the loadCart() method in the ShoppingCart class because the aItems ArrayCollection that populates all the controls is built in this class. You will build the ArrayCollection from the data structure stored in the shared object.

4.

At the end of the class, add a skeleton of the loadCart() method, data typed as void, and use the getLocal() static method of the SharedObject class to read the existing cartInfo SharedObject. Store the shared object in a variable with the name of soCart.

public function loadCart():void{    this.soCart = SharedObject.getLocal("cartInfo"); } 


5.

Add conditional logic that ensures that the aCart variable is not undefined.

if ( this.soCart.data.aCart != undefined ){ } 


The data property of a new shared object does not contain any properties until you add them. Before you attempt to use the aCart data that might be stored in the shared object, you need to ensure that it exists.

6.

Within the conditional logic, create a new local variable with the name of len and obtain the length of the aCart array stored in the data property of the soCart SharedObject, as follows:

var len:int = this.soCart.data.aCart.length; 


data is the only property of the SharedObject class and is where all the data stored in the shared object is accessed. You are accessing the array in this case and can use all Array properties and methods.

7.

Next, set up a looping structure that will loop through the aCart array stored in the soCart shared object.

for (var i:int=0;i<len;i++){ } 


This will loop through the aCart array stored in the shared object. You will loop through this data structure and place the resulting data structure in the aItems ArrayCollection, which has already been bound to all the controls. Data stored in shared objects can be only native ActionScript data structures, so you will need to convert these objects into the value objects that aItems is expecting.

8.

Create a new instance of the Product class using the static buildProduct() method. Pass the method the Product object stored inside of the aCart array.

var myProduct:Product = Product.buildProduct (this.soCart.data.aCart[i].product); 


The buildProduct() method will build a new Product value object based on the Product class you wrote earlier. You must do this because all value objects stored in a shared object are automatically converted into native data structures. Unless you convert these objects back into the appropriate value objects, you will receive a type coercion error when you try to place the objects into the aItems array, which is linked to the visual controls.

9.

After building the Product, define a new quantity of type int from the quantity property stored in the shared object.

var myQuantity:int = this.soCart.data.aCart[i].quantity; 


To define a new ShoppingCartItem, you must define a Product object, which you did in the previous step. You also must define a quantity, which is stored in the shared object. The quantity property in the shared object is untyped, and the ShoppingCartItem requires a variable that has the type of int, so you must define it again here.

10.

Define a new ShoppingCartItem and pass the constructor the myProduct Product object you created in the previous two steps.

var myItem:ShoppingCartItem = new ShoppingCartItem(myProduct, myQuantity); 


The aItems array is an ArrayCollection of ShoppingCartItem value objects, and you must re-create these value objects to avoid type coercion errors because these value objects are not stored as value objects in the shared object. They are stored as a native ActionScript array of objects.

11.

Still within the for loop, call the addItem() method on the class you are currently working on and pass it the myItem ShoppingCartItem. The final loadCart() method should look as follows:

public function loadCart():void{    this.soCart = SharedObject.getLocal("cartInfo");    if ( this.soCart.data.aCart != undefined ){       var len:int = this.soCart.data.aCart.length;       for (var i:int=0;i<len;i++){          var myProduct:Product = Product.buildProduct(this.soCart.data.aCart[i].product);          var myQuantity:int = this.soCart.data.aCart[i].quantity;          var myItem:ShoppingCartItem = new ShoppingCartItem(myProduct, myQuantity);          this.addItem(myItem);       }    } } 


To build the aItems ArrayCollection, you will use the addItem() method of the ShoppingCart. This method checks to see whether the item is already in the cart, manages the quantity of each item, and updates the subtotals of each item.

12.

At the end of the class, add a skeleton of the clearCart() method data typed as void. Within the method, call the aItems.removeAll() method, the soCart.clear() method, and the calcTotal() method of this class.

public function clearCart():void{    aItems.removeAll()    soCart.clear();    calcTotal(); } 


This method empties all the items from the aItems ArrayCollection, clears the contents of the soCart shared object, and forces the shopping cart to recalculate the total. You will call this method when the user finishes the ordering process.

13.

Open views/ecomm/Checkout.mxml.

You will change the saveOrderResult() method to use your new clearCart() method.

14.

Find the saveOrderResult() method. Change the first line, which currently sets the cart.aItems to a new ArrayCollection, to call your clearCart() method on the cart object instead. The final saveOrderResult() method should look as follows:

private function saveOrderResult(event:ResultEvent):void{    this.cart.clearCart();    Alert.show(event.result.getOrderInfoHeader());    var o:Event = new Event("checkOutComplete");    this.dispatchEvent(o);    checkoutNav.selectedChild=billingInfo; } 


15.

Save all open files. Run the EComm application. Add some items to the shopping cart and view the cart. Click the Save For Later button. Close the browser, restart the application, and view the cart again. Finally, complete the order process.

When you restart the application and view the shopping cart, you should see that the shared object has been read and has populated the DataGrid with the items previously in the shopping cart. This information was written to the client machine in the shared object. When you complete the checkout process, the shopping cart and the shared object are both cleared. If you view the cart again, the items will be gone.




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