Getting Started with Adobe Flex 2
Flex, as I'm sure most people know, is a way for programmers to create Flash movies
By: Jeffry Houser
Jul. 17, 2007 02:30 PM
The code, once again, goes inside the application block. The RemoteObject tag has an ID, which allows us to access it in code, a destination that is a special distinguisher, and the source. The source is the Web location of your CFC. When accessing the CFC remotely, it must be Web accessible (unless you change settings to allow you to access CFCs via a ColdFusion mappings, but such a configuration is beyond the scope of this article). Inside the RemoteObject block there is one method that we respond to: "GetHello". The mx:method tag accepts two arguments: a name and the result. The name is the name of the method on the CFC. The result is the name of a local method that will be called when the Flash Player gets the results from calling that event.
The next step in our code base is to write the GetHello_Handler, which is written in ActionScript. You can put ActionScript in a MXML page using the mx:Script tag:
After the script tag comes the CDATA. This tells the XML parser to ignore the text in the script tag. ActionScript is not a valid XML dialect. (I'm not saying that's bad, though.) Then I import the two objects that are used in the function. Importing objects in this manner is not common in ColdFusion development, but if you've worked with older versions of ActionScript or Java, you've probably seen it. It just says, "I need this object, so make it available to me."
The function should look similar to a CFScript function; I specified the function as private. Then comes the function keyword, followed by the name of the function. Next comes the list of arguments. This function only has a single argument, the ResultEvent. Then comes a colon followed by the return type. This function doesn't return anything. The one line of code takes the result from our function call, translates it to a string, and assigns it to the text of our Result label. It sounds more complicated than it actually is.
Type in the code (or copy and paste from the Web version), compile it, and execute it. You should see something similar to Figure 1. Click the button. Unfortunately nothing happened as the button was not told what to do yet. A click event needs to be added to the button. The new button code will look like this:
<mx:Button x="154" y="56" label="Get Hello" click="helloWorld.GetHello()"/>
The click event refers to the helloWorld remote object and says, "Execute the GetHello" method on that object. You should recognize this syntax for calling the method from your use of CFCs inside ColdFusion. When the button is clicked, the Flash player goes to the remote object and calls the method. When the method result is returned, the Flash player looks for the "mx:method" tag and executes the result function. Recompile the code and try it out. Click the button and you should see the Hello World text display next to the button (see Figure 2).
Reader Feedback: Page 1 of 1
Latest Cloud Developer 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