Creating and Deleting with the Data Management Service


You have seen how to retrieve data using the fill() method, and how to update data with an editable DataGrid. You will now create a small application to create and delete products.

1.

Copy the file DMS_2.mxml from your Lesson21/start directory to driveroot/fds2/jrun4/servers/default/flex/flex2tfs. Return to Flex Builder, open that file, then run the DMS_2.mxml application.

Click on one of the products in the DataGrid, and you see that it populates the form and enables the Delete button.

This is the starting file for creating and deleting data with the Data Management Service. The file contains nothing you haven't already done in this book; the only FDS functionality used is the fill() method.

2.

Locate the deleteProduct() function. As the first line of code in the function, use the deleteItem() method of the ds DataService object to delete the selectedItem from the DataGrid.

ds.deleteItem(prodGrid.selectedItem); 


3.

As the second line of code in the function, call the resetForm() method.

4.

Run the application. Select a product and click the Delete button.

You see that the product is removed from the DataGrid. Refresh the page to be sure it is removed from the data store.

The product disappeared from the DataGrid upon deletion because the product was part of the ArrayCollection: it was removed from the ArrayCollection, and the DataGrid's dataProvider uses a binding to the DataGrid; hence, the DataGrid shows the latest and greatest data.

5.

Locate the createProduct() function. As the first lines of code in the function, assign the product name and cost values from the form to the product variable. You will have to use the Number() function to convert the text gathered in the cost TextInput control to be able to assign it to the Number typed cost variable from the SmallProduct value object.

product.prodName=productNameInput.text; product.cost=Number(costInput.text); 


This builds a new SmallProduct object to be inserted into the data store via the Data Management Service.

6.

Use the createItem() method of the ds DataService object to create the product item in the data store.

ds.createItem(product); 


This will insert the new product in the products ArrayCollection, as well as send the new product to the server side to be inserted into the data store.

7.

As the last line of code in the function, call the resetForm() method. Also, check to be sure your completed createProduct() function appears as shown.

The resetForm() method simply clears the form and resets the application to its beginning state.

private function createProduct():void{    product.prodName=productNameInput.text;    product.cost=Number(costInput.text);    ds.createItem(product);    resetForm(); } 


8.

Run the application. Input new data for a product and click the Add button.

You see that the new product is added to the DataGrid. Refresh the page to be sure it was also added to the data store.




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