Importing a Service into Flex



The relationship between client code and server code in Flex differs from the traditional relationship in a server template.

In a traditional relationship, a server template mixes server code with client code. When the client queries a database, it dynamically embeds HTML code with returned data.

Flex, however, separates client code from server code. The remote service returns only data. Flex binds the returned data to user interface components in the client application.

This tutorial shows how to create a client application that imports a ColdFusion service. It also shows how to bind the data returned from the service to a Flex user interface component. Finally, the tutorial examines the generated code.

Import a service and bind returned data to application components

This tutorial assumes that you have completed the earlier tutorials, Building a basic application, and Creating ColdFusion services for client applications.

  1. In Flash Builder, select File > New > Flex Project.

  2. Specify HelloCF for the project name, set Application Server Type to ColdFusion, and specify ColdFusion Flash Remoting.

    View full size graphic
    Specify project name and server type

  3. Click Next. Verify your ColdFusion server configuration and click Validate Configuration.

  4. For Output Folder, specify the HelloCF folder you created previously for the Hello project.

    Flash Builder suggests a default location of HelloCF-debug for the output folder. Use the location from the previous tutorial that already contains HelloWorld.cfc.

  5. Click Finish.

    The Flash builder source editor opens to HelloCF.mxml.

  6. Copy the Label component you created in Hello.mxml and paste it into HelloCF.mxml. Save and run HelloCF.mxml.

    You can copy the component from either Design View or Source View of the editor.

    Running the HelloCF.mxml application gives the same results as Hello.mxml.

    HelloCF application

  7. In the Data Services View of Flash Builder, select Connect To Service.

    Data Services View

  8. Select ColdFusion as the service type and click Next.

    View full size graphic
    Select service type

  9. Click Browse and navigate to the HelloService.cfc you previously created. Flash Builder names the service HelloService.

    View full size graphic
    Importing a ColdFusion Service

  10. Click Finish. Provide your credentials to log in to the ColdFusion database.

    The Data Services View now displays your ColdFusion service.

    The Data Service View provides a picture of remote services. This view becomes useful when you configure types for data returned by a service call.
  11. Select Design to open Design View, and then select the RichEditableText component.

  12. In the Properties view, select the Bind to Data button for the RichEditableText component.

    View full size graphic
    Binding data to a component from the Property Inspector

    Select OK to replace the current text with the text returned from a service operation.

    Flash Builder provides various ways to bind data to a selected component. In addition to the Property Inspector, you can bind to data from the context menu. You can also select Bind to Data from the Flash Builder Data menu.
  13. In the Bind to Data dialog, make sure that New Service Call is selected. For Operation select getMessage. Click OK.

    Select the operation for binding data

    View full size graphic
    Design View showing data binding

  14. Save and run the application.

    The Text component displays the message retrieved from the HelloService.

    View full size graphic
    Application accessing remote service

Examine the generated code

When you imported the ColdFusion service into a client application, Flex generated client code that accessed the ColdFusion service. Flex also generates code when you bind operations to application components.

There are three parts to the generated code:

  • Event handler

  • Service call and responder

  • Data binding

This part of the tutorial walks you through the Flex code generated in the previous tutorial. There are no “steps” to this part of the tutorial. Just a description of the code generation.

Event handlers

Flex uses event handlers to trigger a service call.

Flex code is not processed “top to bottom,” as in server templates. Instead, applications built with Flex listen for events to trigger responses.

By default, Flex uses the creationComplete event on a component to trigger a service call.

Examine the RichEditableText component in the HelloCF.mxml source code:

<s:RichEditableText x="70" y="60" 
    text="{getMessageResult.lastResult}" 
    fontWeight="bold" fontSize="24" fontStyle="italic" 
    color="red" id="richEditableText" 
    creationComplete="richEditableText_creationCompleteHandler(event)"/>

The creationComplete event fires after the application creates the component. For this RichEditableText component, creationComplete calls an event handler that Flash Builder generated for the event. Examine the code for the event handler, which is placed inside of a Script block:

<fx:Script> 
    <![CDATA[ 
        import mx.events.FlexEvent; 
        import mx.controls.Alert; 
 
        protected function richEditableText_creationCompleteHandler(event:FlexEvent):void 
        { 
            getMessageResult.token = helloService.getMessage(); 
        } 
 
    ]]> 
</fx:Script>

The creationComplete event handler calls the getMessage operation from the ColdFusion service.

Other events, such as a Click event on a button or a selectionChange event of a list, can be used to trigger service calls.

Service calls and responders

When you add a service, Flex creates a component containing methods that call the service operations. When you invoke one of the methods, the component calls the corresponding service operation over the network using the appropriate protocol.

Service calls are associated with CallResponders. Data returned from a service call is placed in the lastResult attribute of the CallResponder. CallResponders allow service calls to operate asynchronously. The client application can continue to be responsive while the service call is being processed.

Examine the generated service call and responder:

<fx:Declarations> 
    <s:CallResponder id="getMessageResult"/> 
    <helloservice:HelloService id="helloService" 
        fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" 
        showBusyCursor="true"/> 
</fx:Declarations>

When you imported the HelloService service, Flex created the HelloService class. This class contains methods to invoke the getMessage(), getLocales(), and getLocalizedMessage operations of the service.

The CallResponder, identified by the getMessageResult id, provides access to the returned data.

Data binding

Flex data binding allows you to assign the value of the lastResult attribute of a CallResponder to a UI component. When the last result updates, Flex automatically updates the component.

Flex uses curly braces {} to bind data to an attribute of a component. In this example, Flex binds the lastResult value to the text attribute of the RichEditableText component.

Examine the data binding for the RichEditableText component:

<s:RichEditableText x="70" y="60" 
    text="{getMessageResult.lastResult}" 
    fontWeight="bold" fontSize="24" fontStyle="italic" 
    color="red" id="richEditableText" 
    creationComplete="richEditableText_creationCompleteHandler(event)"/>

The data returned to the CallResponder, getMessageResult.lastResult, is automatically assigned as the text attribute for the RichEditableText component.

Code generation summary

In summary, Flash Builder generates code to access the imported service in the client application:

  • After Flex creates the RichEditableText component in the running application, the creationComplete event fires, calling a service operation.

  • The data returned from the service call is placed in the lastResult attribute of the associated CallResponder.

  • The text attribute of the RichEditableText component, which is bound to the lastResult attribute, updates with the returned data.