This article is the first installment in a series dedicated to simplifying client server integration using Flash Remoting. In this article we review the process of loading data from a database and displaying it in the DataGrid component. The example will demonstrate 2 techniques to display the data. The first is automatic using the dataProvider property of DataGrid. The second uses ActionScript and ResultSet API to add ResultSet records programmatically.
EXAMPLE WALKTHROUGH
The first iteration of the example application is rather simple. We plan to evolve this example over time to demonstrate advanced concepts of working with server-side data fetched from data sources.
The example application uses the same server-side code and database available in the distribution of FlashORB.Net. When 'Load Data' button is clicked, a flash remoting invocation is sent to the server side. The server-side object fetches all records from the Customers table from the Northwind database (also available in the FlashORB.Net distribution). Since the server-side method returns an instance of System.Data.DataSet, FlashORB automatically serializes it so it appears as ActionScript ResultSet to the client side.
When the result is received, the client application checks how the data grid component should be populated. The radio button selection specifies the method. The data provider method is the simplest - assign the result object to the dataProvider field of the DataGrid component and the data is populated automatically. The second option uses ResultSet ActionScript API to populate the DataGrid with the records fetched from the server.
The code to the left is the response handle for the flash remoting method call that fetches data from the database. The 'result' object is an instance of ResultSet. It contains the data delivered from the server. Also, it holds a connection to the server in case when the data is returned in pages and additional pages must be fetched after the initial response.
FlashORB's supports advanced database paging. The product can be configured to return any number of records in the initial response or to return the entire set. (The default setting is 10).
To provide additional information on the status of loading records, ResultSet class support a concept of Views. A developer can register a view object that will receive callbacks reporting when records are fetched or available for display. Line #3 in the code to the left registers a view object with the ResultSet.
View classes must implement the modelChanged( event ) method (lines ##24-40). The method is called when records are being fetched or become available.
Lines ##14-20 programmatically add records from the ResultSet to the DataGrid ('customers' is the variable name representing the DataGrid component). When a record is not available locally, the getItemAt() method returns the "in progress" string, indicating that the record is being loaded. As soon as the record is loaded, the modelChanged() method is called with the "updateItems" eventName (line #29). When the "updateItems" event occurs, it carries additional information on which items became available. That information is captured by the firstItem and lastItem properties of the event object (line #35). At that point, the records can be safely added to the DataGrid.
Future steps:
Advanced paging techniques primer
Updating the server with records inserted on the client side
Updating the server with records deleted on the client side
Updating the server with the records modified on the client side