Chapter 26. Connecting Your Project to Live Data

IN THIS CHAPTER

  • Setting up a server

  • Changing the project type

  • Connecting to data services

  • Binding data to a component

Your design comp likely contained a representation of data. In your Catalyst project, you converted that representation into a data list and, with the help of the Design-time Data panel, displayed sample data. Now, you will be able to connect the project to live data, generated by the server.

Flex projects run entirely on the client's side; that is, they run in Flash Player through a Web browser or on a user's desktop via AIR. As they do not run on the server, they have no direct access to server-side resources such as databases.

In order for your application to display live data, you need a script running on the server that can communicate with the database, retrieve the desired data, format it appropriately, and send it to Flex.

You can write this script in any one of a number of languages and rely on varying technologies. Whether you or your developer is proficient in Adobe ColdFusion, PHP, Microsoft ASP.NET, Ruby on Rails, or almost any other server-side technology, you will be able to leverage it to get data into your Flex application.

Setting Up a Local Testing Server

A local testing server is a Web server installed on your personal computer that you can use to simulate live connections with the database. While there are quite a few steps involved with downloading, installing and configuring the server, the process is fairly simple and will enable you to test your pages and further develop your applications as needed.

Download and install ColdFusion

Adobe ColdFusion provides a set of powerful, easy-to-learn and easy-to-use tools for developing data-driven Web applications. As a commercial product, it does need to be purchased by your company or Web host in order for it to be deployed on a live, production Web server. However, Adobe makes a Developer Edition available, free of charge, to be installed and used in testing environments.

  1. Using a Web browser, go to www.adobe.com/products/coldfusion.

  2. Click Get the Free Trial (see Figure 26.1). A login screen appears.

    Note

    The most recent release of ColdFusion as of this writing is version 9, released in October of 2009. The process shown in these steps will work with ColdFusion MX7, ColdFusion 8, or ColdFusion 9, so if you have already installed one of those earlier versions, you need not re-download and reinstall ColdFusion 9.

  3. Log into Adobe's Web site in order to download software. If you have an Adobe account, type your username and password. If you do not have an account, you can click the link on the page and create a free account, and then return to this page and log in (see Figure 26.2).

    The Adobe ColdFusion home page

    Figure 26.1. The Adobe ColdFusion home page

    The login page

    Figure 26.2. The login page

  4. Select the version of ColdFusion that is appropriate for your operating system and click Download (see Figure 26.3). The File Download dialog box appears.

    The download page for ColdFusion

    Figure 26.3. The download page for ColdFusion

  5. Click Save (see Figure 26.4).

  6. Navigate to a directory on your computer into which you want to download the file and click Save.

  7. When the download completes, navigate to the directory into which you downloaded the file and double-click it to launch the installer. The first step of the installer informs you that ColdFusion is going to be installed.

  8. Click Next (see Figure 26.5).

    Saving the download

    Figure 26.4. Saving the download

    The first step of the ColdFusion installer

    Figure 26.5. The first step of the ColdFusion installer

  9. After reviewing the license agreement, click Next (see Figure 26.6).

    The ColdFusion license installer license agreement

    Figure 26.6. The ColdFusion license installer license agreement

  10. Click the option to install the Developer Edition.

  11. Click Next (see Figure 26.7).

  12. Select a configuration for ColdFusion. For a local testing server, Server configuration option, which is the default option, works best.

  13. Click Next (see Figure 26.8). The next screen displays a list of subcomponents to install with ColdFusion.

    Choosing to install the Developer Edition

    Figure 26.7. Choosing to install the Developer Edition

    The Server Configuration screen

    Figure 26.8. The Server Configuration screen

  14. Leave the default selections and click Next (see Figure 26.9).

    The Subcomponent Installation screen

    Figure 26.9. The Subcomponent Installation screen

  15. Select the directory into which you want to install ColdFusion. Unless you have a particular reason to choose something else, leave it at the default.

  16. Click Next (see Figure 26.10). The next screen allows you to specify that you will be using the stand-alone version of ColdFusion.

  17. Select the Built-in web server (Development use only) option.

  18. Click Next (see Figure 26.11).

    Choosing the installation directory

    Figure 26.10. Choosing the installation directory

    Setting ColdFusion to use its built-in Web server

    Figure 26.11. Setting ColdFusion to use its built-in Web server

  19. Enter a password and type it again to confirm. This password will be used by you when you need to log into the ColdFusion administrator to configure your server, so be sure to use a password that you will remember (see Figure 26.12).

    ColdFusion uses the Remote Development Service to allow external editors such as Adobe Dreamweaver and Adobe ColdFusion Builder to have access to the server to simplify development. On a development or testing machine such as the one you are setting up, you should enable RDS and set up a password to control access. You should not enable RDS on a live production server.

    Entering a server password

    Figure 26.12. Entering a server password

  20. In this case, select the Enable RDS option and type, then confirm, a password (see Figure 26.13). The installation wizard offers a summary of the settings.

  21. When you are ready, click Install (see Figure 26.14). The installer will likely take several minutes.

    The Enable RDS step of the wizard

    Figure 26.13. The Enable RDS step of the wizard

    The summary screen of the installer

    Figure 26.14. The summary screen of the installer

  22. Once the installation is complete, launch the ColdFusion administrator to complete the configuration (see Figure 26.15).

    The Installation Complete page

    Figure 26.15. The Installation Complete page

  23. Click the link provided to launch your Web browser. After a few minutes, the installer will complete and display the ColdFusion administrator login screen (see Figure 26.16).

  24. Log in and explore the administrator if you wish, using the password you created during the installation process (see Figure 26.17).

The ColdFusion administrator login screen, which displays when the installation and configuration are complete

Figure 26.16. The ColdFusion administrator login screen, which displays when the installation and configuration are complete

The ColdFusion Administrator home page

Figure 26.17. The ColdFusion Administrator home page

You have now installed ColdFusion on your computer, and are ready to begin using it as a back-end resource to connect your project to live data.

Configuring the database

After ColdFusion is installed, you need to set up and configure a database to use for your application. Many relational database systems exist, from desktop applications such as Microsoft Access to enterprise-level solutions offered by Oracle. One of ColdFusion's strengths is its ability to work with almost any database system.

Learning to effectively create, use and administer a relational database is a complicated task that is well beyond the scope of this book. Fortunately, ColdFusion includes a powerful database system, Apache Derby.

Derby offers many of the features found in expensive, enterprise-grade systems such as Microsoft SQL Server, but as an open-source solution, it is available free of charge. However, it does entirely lack a user interface. Therefore, it is necessary to create your own front end to the database. In this case, you can use ColdFusion to create that front end.

Note

You can download the ColdFusion files for configuring the database from the book's Web site (www.wiley.com/go/flashcatalystbible).

  1. Download the ColdFusion database configuration files from the book's Web site (www.wiley.com/go/flashcatalystbible).

  2. Once the files are downloaded, unzip them into the ColdFusion Web root. If you used a default installation on Windows, the root will be at c:ColdFusion9wwwroot, while on the Mac it will be at Applications/ColdFusion9/wwwroot.

  3. Open a Web browser and type http://localhost:8500/catalystbibledbconfig.cfm in the browser's address bar.

  4. Press enter.

  5. Enter the ColdFusion administrator password you set up while installing ColdFusion.

  6. Click Set Up Database. The ColdFusion page will create the necessary settings to configure the database, as well as creating the tables needed for the remainder of the examples here and populate the database with sample data.

    Once complete, a confirmation page will be displayed.

  7. When you get the confirmation page, you can close the browser (see Figure 26.18).

The ColdFusion database configuration completed

Figure 26.18. The ColdFusion database configuration completed

Changing the Project Type

When you create a project in Flash Builder, you must choose when you initially set up the project whether it will be static or use server-side resources.

Catalyst provides no such choice: All projects are assumed to be static.

Set up the project

When you import a Catalyst project that will need server-side resources into Flash Builder, you need to first convert the project to the appropriate type.

  1. Open Flash Builder. If necessary, open a project created in Catalyst or import a new Catalyst project (see Figure 26.19).

    Flash Builder with an open project created in Catalyst

    Figure 26.19. Flash Builder with an open project created in Catalyst

    Note

    See Chapter 25 for details on importing Catalyst projects into Flash Builder.

    Note

    The following exercises assume that you have completed the steps in Chapter 25 to import the project. If you did not do those exercises, you can download a version of the project for this chapter from the book's Web site (www.wiley.com/go/flashcatalystbible).

  2. Click the Data/Services view, located by default at the bottom of the Flash Builder interface.

  3. Click the Connect to Data/Service link (see Figure 26.20) to launch the Flash Builder Wizard that will step you through the process of connecting to a server-side resource. The first step of the wizard allows you to choose the type of service to which you want to connect (see Figure 26.21).

    The Connect to Data/Services link in the Data/Services view in Flash Builder

    Figure 26.20. The Connect to Data/Services link in the Data/Services view in Flash Builder

  4. Click ColdFusion.

  5. Click Next. You will be alerted to the fact that your project is not configured properly (see Figure 26.22).

  6. Click Yes to open the settings dialog box to change the configuration. The Project Properties dialog box opens, with the Flex Server screen selected.

  7. From the Application server type drop-down list, select ColdFusion.

  8. If necessary, select the Use remote object access service option.

  9. Select ColdFusion Flash Remoting.

  10. Set the ColdFusion installation type to Standalone.

  11. Select the Use built-in ColdFusion web server checkbox. Assuming that you installed ColdFusion using the defaults, the Web root and Root URL text boxes should have the correct paths pre-configured.

    The first step of the wizard, where you select the type of service

    Figure 26.21. The first step of the wizard, where you select the type of service

    The configuration warning dialog box

    Figure 26.22. The configuration warning dialog box

  12. In the Compiled Flex application location, type the location where you want the file to be deployed. It must be within the Web root.

  13. Click OK (see Figure 26.23). Flash Builder displays the New Flex Service dialog box.

    The Flex Server configuration dialog box showing the settings necessary to set up a ColdFusion server resource.

    Figure 26.23. The Flex Server configuration dialog box showing the settings necessary to set up a ColdFusion server resource.

    Note

    The Flex 4 Bible contains more details on setting up the server configuration. You can also look in the Flash Builder help files for more information.

  14. To specify the location of the ColdFusion Component (CFC), which will be responsible for retrieving the database information, click Browse and navigate to the directory that contains the CFC you downloaded from the book's Web site (www.wiley.com/go/flashcatalystbible).

  15. Type magazineList as the Service name (see Figure 26.24). Flash Builder should automatically enter services.magazines as the Service package and valueObjects as the Data type package.

  16. Click Next.

    Configuring the ColdFusion service

    Figure 26.24. Configuring the ColdFusion service

  17. If you are prompted for an RDS username and password, type admin as the username and the RDS password you created when you installed ColdFusion (see Figure 26.25). Flash Builder examines the CFC and returns a list of the operations, or methods, available in it. In this example, there is only one such method.

  18. Select the method (see Figure 26.26).

  19. Click Finish. Flash Builder creates a series of ActionScript classes to process the data returned from the CFC.

    The RDS login screen

    Figure 26.25. The RDS login screen

    Selecting the Service Operation

    Figure 26.26. Selecting the Service Operation

  20. In the Package Explorer, expand the services package, then the services.magazineList package to see the ActionScript files created by the wizard (see Figure 29.27).

The ActionScript files created to process the data

Figure 26.27. The ActionScript files created to process the data

Your project is now properly set up, and the necessary ActionScript files have been created.

Configuring the return data

You need to configure the type of data that the service will return so that Flex can properly process it. Once you have completed the steps in the previous section, the Data/Services view will display the getAllMagazines operation.

  1. Select the getAllMagazines operation.

  2. Right-click (

    Configuring the return data
    Selecting the service and clicking the Configure Return Type button in the Data/Services view

    Figure 26.28. Selecting the service and clicking the Configure Return Type button in the Data/Services view

  3. In the Configure Return Type dialog box, ensure that the option Auto-detect return type from sample data is selected (see Figure 26.29).

  4. Click Next. Flash Builder needs to call, or invoke, the service to determine what kind of data will be returned. Some services will require that a parameter be sent to them; for example, a search operation would need a search string. The service used in these examples, however, simply returns all data, so you do not need to provide a parameter.

    Flash Builder will therefore invoke the service and return a list of the data being returned.

    In this example, the service returns a list of magazines and information about them. ActionScript can store multiple values in a single variable, as is needed in this case, in an array. Flash Builder will display a dialog box stating that it cannot automatically determine the property type.

  5. Enter a name for the array, such as MagazinesData (see Figure 26.30).

  6. Click Finish.

The first step in configuring the return type

Figure 26.29. The first step in configuring the return type

Entering the name of the property type for the data.

Figure 26.30. Entering the name of the property type for the data.

Warning

The name of the service and the name of the data type cannot be the same. In this example, the service name is Magazines, so the data type cannot be Magazines.

Binding the Data to the Visual Component

The final step in getting the data into the component is binding the data returned from the service to the component that will display it.

  1. If necessary, open Main.mxml by double-clicking it in the Package Explorer in Flash Builder.

  2. If necessary, click Design to view the file in Design mode (see Figure 26.31).

  3. Check the States view in the top-right corner of the screen to ensure that you are in the HomeState.

  4. Click on the data list component created in Catalyst.

    Viewing the main project file's home state in Design mode, with the data list component selected

    Figure 26.31. Viewing the main project file's home state in Design mode, with the data list component selected

  5. In the Data/Services view, click the Bind to Data button (see Figure 26.32). You can also drag the getAllMagazines operation from the Data/Services view and drop it on the list.

    In Catalyst, you assigned sample data to the control. Flash Builder now needs to replace that dummy data with the real information from the service, so it will display a warning message confirming that you want to perform this operation.

    The Bind control to data button in the Data/Services view

    Figure 26.32. The Bind control to data button in the Data/Services view

  6. Click OK. A dialog box appears (see Figure 26.33).

  7. Confirm that the correct operation is being used. The dialog box should already be filled out correctly, but if necessary, you can select the MagazineList service and getAllMagazines operation (see Figure 26.34).

  8. Flash Builder will want to automatically assign the data to a label, but click in the drop-down and select the empty option. This enables you to use a custom component to display the data.

  9. Click OK.

    The warning that the dummy data is going to be replaced

    Figure 26.33. The warning that the dummy data is going to be replaced

    The Bind to Data dialog

    Figure 26.34. The Bind to Data dialog

  10. Right-click (

    The Bind to Data dialog
  11. Ensure that the Magazines data type is selected, and that the Make form editable option is not (see Figure 26.35).

  12. Click Next. The wizard displays the data being returned by the service.

    The first step in the Generate Form wizard

    Figure 26.35. The first step in the Generate Form wizard

  13. Remove the checkmark in the box next to magazineID, as you do not want to display this.

  14. Ensure that magazine_title and price are set to Text controls, while cover_picture is set to Image (see Figure 26.36).

  15. Click Finish. The form generated by the wizard is placed in the list.

  16. Click each text control and use the Properties view to adjust the font settings as desired.

Configuring the display types

Figure 26.36. Configuring the display types

The completed data list, bound to live data from the server

Figure 26.37. The completed data list, bound to live data from the server

Summary

This chapter took you through the process of setting up a testing server on your computer and configuring your Catalyst project to display live data from the server. You learned:

  • How to download and install ColdFusion

  • Change the project type in Flash Builder to use server-side resources

  • Set up the project to call a service

  • Bind the data returned from the server to a component

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.19.174