|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
CF & Flex Flex Your ColdFusion Muscles with Adobe Flex
Building Rich Internet Applications couldn't be easier with ColdFusion and Flex
By: Matthew Woodward
May. 12, 2007 06:00 PM
As time went on, however, users demanded more and more from HTML, which led to the rise of Web applications. At first the users were pleased with the ability to shop and do their banking online, but they soon became frustrated. "These Web applications don't behave like my desktop applications!" they cried. "I'm sick of waiting for page reloads every time I click on something. I want a better experience!" Web developers have done their best to meet these increasing demands through a rather clumsy mix of HTML and other technologies such as JavaScript, DHTML, and Flash. They have met with varying degrees of success. Because of the nature of these technologies and their inherent limitations, developers are still prevented from providing desktop-like application functionality and rich user experiences within the Web browser. The time has come for a completely new Web application paradigm; one that combines the power of the Internet with the user experience of a desktop application. Let There Be Flex Macromedia's RIA initiative began with the evolution of the Flash IDE into a stronger application development tool. Flex is another step forward for RIAs and offers many development advantages over the Flash IDE. Using Flex, developers who may be uncomfortable with the Flash IDE can quickly build RIAs in their favorite code editor. Because of the code-centric nature of Flex applications, there's no need to sacrifice enterprise application development methodologies and architectures. Best of all, Flex applications run in the extremely ubiquitous Flash player so we can be certain our Flex applications will run practically anywhere. As if this weren't compelling enough, Macromedia recently announced Flex 1.5. This adds exciting new features to the already-rich Flex toolset such as vastly improved charting and graphing capabilities, more advanced UI components, and simpler application skinning. The performance has also been greatly improved, and with Flex 1.5, developers can also integrate their Flex applications with Macromedia Central. If you haven't already checked out Flex, now is a great time to do so. For more information on the new features of Flex 1.5, visit www.macromedia.com/software/flex/. In this article I'll briefly address RIAs and discuss the types of applications for which they are especially well-suited, introduce you to Flex, and discuss how Flex and ColdFusion can be used in conjunction with one another. I'll also explain how Flex can be used to put a completely new face on an existing ColdFusion application. Improving the User Experience Flex solves this problem by allowing developers to create a complete Web application in a single-screen model. Flex applications are able to refresh discrete portions of the screen or change from one view to another without having to load a new page in the browser. Flex also offers more advanced UI components when compared with HTML, allowing for vastly improved functionality that would be difficult or impossible to achieve with HTML. Also, because Flex applications run in the Flash player, the headaches of browser inconsistencies with DHTML are completely avoided. The end result is a far richer, more interactive experience for your users. For additional information on RIAs, visit the RIA section of Macromedia's Web site at www.macromedia.com/resources/business/rich_internet_apps/. Obtaining Flex Macromedia also recently announced that a noncommercial, noninstitutional Flex license will soon be made available at no cost. This means you will be able to use Flex as a learning tool or for building personal applications and make these applications accessible to others. This is fantastic news, and I encourage you all to take advantage of this. Read Macromedia's FAQ for more information (www.macromedia.com/software/flex/productinfo/faq/#ancni). You may apply for a noncommercial, noninstitutional Flex license at www.macromedia.com/go/flexlicense. Because there are several excellent resources covering the installation and configuration of Flex, I will not be addressing these issues. If you need assistance installing Flex, visit the Flex section of Macromedia DevNet at www.macromedia.com/devnet/flex. Or you can go to the Flex Documentation page at www.macromedia.com/support/documentation/en/flex/. Flex Basics Concerning the languages used to write Flex applications, in similar fashion to ColdFusion, Flex applications are created by using a rich set of extremely powerful tags. These tags are written in a flavor of XML called MXML, and this serves as the base language for the creation of Flex UIs. To unleash the true power of Flex, however, ActionScript 2.0 is also used. Think of MXML as the tags used to build the basic structure of a Flex UI and ActionScript as the language that brings the UI to life (although there's a lot you can achieve with MXML alone). If you have experience with scripting in Flash, you will already be familiar with ActionScript. If not, ActionScript is an ECMAScript-compliant language, so the Java or JavaScript skills possessed by many Web developers will dramatically ease the learning curve. Macromedia's LiveDocs for Flex is at http://livedocs.macromedia.com/flex/15/flex_docs_en/index.html. This is an extremely handy reference as you build Flex applications. Where Does ColdFusion Fit In? ColdFusion, on the other hand, excels at providing powerful back-end functionality, but the presentation layer is not addressed directly by ColdFusion. This is precisely where ColdFusion fits into the Flex equation. Through the use of CFML pages and ColdFusion Components (CFCs) exposed as Web services, we can use our existing ColdFusion skills to create back-end components and easily leverage these components with Flex. Because Flex functionality can be exposed as a Java tag library, you can also combine Flex and JSP or CFML code within a single page, but this is beyond the scope of this article. You can read more about this on Christopher Coenraets's blog: www.markme.com/cc/archives/004021.cfm. As you'll soon see, using Flex with ColdFusion is an extremely powerful combination of technologies that allows us to build compelling RIA experiences with a minimal amount of code. Case Study: A Forum Facelift with Flex Galleon comes complete with user-management features and an administration module. However, in the interest of focusing on Flex basics, these portions of Galleon have been omitted from the Flex application. Session management and some of the finer points of Flex development will be left for a future article. But armed with the knowledge you'll gain from this article, you can complete the Flex UI for Galleon on your own! (Consider that a homework assignment.) The ColdFusion Code We will also be using a simple CFML page in the Flex application to illustrate how Flex can leverage CFML pages as HTTP services. There is also a "gotcha" in Flex when XML is retrieved via a Web service call. To make a long, hair-tearing story short, if you're using ColdFusion to generate XML and you want to use this XML in your Flex application, you must use a CFML page as opposed to a CFC function that returns XML. (This is a known issue that should be fixed in future versions of Flex.) We'll use an HTTP service call to a CFML page to populate the forum navigation tree in our Flex application (see Listing 2). The Flex Code Our Flex application is broken down into four files. FlexForums.mxml (see Listing 3) is the main application file, and this is what's called in the Web browser to launch the application. FlexForums_script.as (see Listing 4) is an ActionScript file that contains all the functions that are used in the application. This could have been included in a script block within FlexForums.mxml. However, separating the ActionScript code from the MXML file makes for cleaner code organization. Two additional MXML files, ThreadForm.mxml and MessageForm.mxml (see Listings 5 and 6), contain the forms used for adding new threads and posting messages to the forums. Setting Sail on the Galleon RIA The new Flex face we've put on Galleon is the familiar interface of many desktop newsreader programs. The default "Halo" style of Flex creates an attractive, clean UI with no graphic design work whatsoever. All the Flex components as well as the application itself are quite easily skinnable using standard CSS syntax. Thus, you're by no means stuck using this look for all your applications. Apart from the rich interface itself, the most compelling aspect of the Flex application is its behavior. When the user clicks on a specific forum, for example, the messages are retrieved from the database and the data grid is updated without requiring a refresh of the entire screen. When new threads or messages are posted by the user, the data grids automatically refresh immediately and display the latest data. Navigating between forums, threads, and messages is all immediate and again requires no reloading. Even retrieving specific messages is a simple, quick call to the server that updates one portion of the Flex display without having to refresh the entire page. This creates a far more cohesive, seamless experience for the user. Retrieving Data From ColdFusion Calling a CFML Page as an HTTPService <mx:HTTPService id="conferenceFeed" url="http://192.168.1.105:8103/cfusion/forums/getConferences.cfm" showBusyCursor="true" /> The ID attribute simply gives the service a handle that can be used within Flex, the URL is the location of the CFML page, and the showBusyCursor attribute tells Flex to change the cursor into an hourglass while the service is being called to give visual feedback to the user. The XML returned from the CFML page is then set as the data provider for the navigation tree and Flex handles the rest.
<mx:Tree id="forumTree" height="100%" width="20%" showDataTips="true" dataTipField="description"
change="handleTreeChange(event);" dataProvider="{conferenceFeed.result.node}" />
The other item of interest is the change attribute, which tells Flex to call a function when a change is made to the tree. When the user clicks on a forum in the tree, this change triggers the handleTreeChange function, and this function subsequently calls the method that loads the thread data. The details of this operation can be seen in the handleTreeChange function in the FlexForums_script.as file (see Listing 4). Calling a CFC Method as a Web Service As an example of this, let's take a brief look at how we use a call to a CFC to populate a Flex DataGrid (see Figure 2). The DataGrid is an extremely rich component with a large amount of out-of-the box functionality such as sortable and resizable columns, so it provides a very powerful way to present data. To fill the DataGrid with data, we simply set the result of the call to the Web service (which returns a ColdFusion query object) as the data provider for the DataGrid. Flex dutifully displays the data retrieved from ColdFusion. In our main MXML file we must first define our Web service, which in this case is a service that has been named "FlexForums" in the Flex configuration file as opposed to being called by a URL: <mx:WebService id="forumWS" serviceName="FlexForums" showBusyCursor="true" /> Once the Web service is defined, in our ActionScript code we simply call the Web service method that returns the thread data: forumWS.getThreads(forumId); One item to note is that because ColdFusion is case insensitive, it returns the column names of the ColdFusion query to Flex in caps, so we must use all capital letters to refer to the column names in our DataGrid columns (see Listing 7). Again, because we have set the dataProvider of the DataGrid to be the result of the getThreads call, the DataGrid is automatically updated with new data upon completion of the Web service call. Simple yet extremely powerful! Next Steps Conclusion Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||