ColdFusion 9.0 Resources |
Building a simple applicationThe following procedure describes how to build a simple server-side ActionScript application. The example application, a corporate personnel directory, uses the NetServices object to connect to the personneldirectory server-side ActionScript. The personneldirectory server-side ActionScript retrieves data from a ColdFusion data source and returns the results to the SWF file as a RecordSet object. Note: The server-side ActionScript application that
you create provides the back-end services in an application.
This example requires the following:
Create the application
Writing the server-side ActionScript functionThe example here creates a search function that performs a simple search operation against a ColdFusion data source. This function accepts two arguments, firstName and lastName, and returns any records found that match these arguments. Create a server-side ActionScript function Create a server-side
ActionScript file that contains the following code:
//search takes firstName lastName arguments function search(firstName, lastName) { searchdata = CF.query({datasource: "bigDSN", sql:"SELECT * from personnel WHERE fname = firstName AND lname = lastName"{); if (searchdata) return searchdata; else return null; } Creating the SWF movie interfaceThe SWF movie interface example here consists of one frame with a variety of text boxes and a submit button.
Submitting user data to the Flash Remoting serviceTo send data to server-side ActionScript, create a function that passes the data from the SWF movie to server-side ActionScript. The search function, applied at the frame level, collects the user-entered data from the firstName and lastName text boxes and passes the data as function arguments to the directoryService object, which is created when the SWF movie connects to the Flash Remoting service. For more information, see Checking for a Flash Remoting service connection. The following is a Flash ActionScript example: #include "NetServices.as" function search() { // The search() method is defined in the server-side AS file directoryService.search(firstName.text, lastName.text); dataView.setDataProvider(null); status.text = "waiting..."; } Reviewing the codeThe following table describes the code and its function:
Capturing Flash Remoting service resultsWhen you create a function that calls a server-side ActionScript function, also create a function to handle the data returned by server-side ActionScript. Define the function with the same name as the function making the initial call, but you append _Result to the name. For example, if you create a function called basicQuery to return query data, you define a results function to handle returned data; declare the results function as basicQuery_Result. In the following example, the results function search_Result supplies the record set to the dataView.setDataProvider function: function search_Result(resultset) { dataView.setDataProvider(resultset); status.text = (0+resultset.getLength())+" names found."; } Reviewing the codeThe following table describes the code and its function:
Checking for a Flash Remoting service connectionTo ensure that the SWF movie is connected to the Flash Remoting service, you use an if statement; for example: if (inited == null) { inited = true; NetServices.setDefaultGatewayUrl("http://localhost:8500/flashservices/ gateway"); gateway_conn = NetServices.createGatewayConnection(); directoryService = gateway_conn.getService(personneldirectory, this); status.text = "Type into the text boxes, then click 'Search'"; } In this example, the inited variable is evaluated for a value. If inited is null (not connected), the movie connects to the Flash Remoting service using the NetServices object. For more information about connecting to the Flash Remoting service, see Connecting to the Flash Remoting service. |