Getting Started with Flex and CF

Posted By : todd sharp Posted At : November 21, 2006 9:24 PM Posted In: CFCs, Flex, ColdFusion

2

I've finally found a little time to actually sit down and give some love to this blog. All of my free time lately has been spent writing code, but it's good to actually sit back for a minute and document some of my experiences. If you're a reader of my blog, you know that I've recently started working with Flex after spending about 2 years with Flash Forms. Don't get me wrong, Flash Forms are slick. I still have many applications built with Flash Forms that are up and running, but there's something inside me now that just wishes I could rewrite every single one of them in Flex 2. I feel like I've been writing Flex app's for years, even though I'm still very naive to much of it. Like someone recently said in a comment thread that I read today - much of what you will learn to do in Flex is very easy to implement - it's just figuring out how to do it that's the pain sometimes. The good part of this learning curve is that once you figure something out, it's usually so dead simple that it's very easy to remember. I could be wrong, but I don't feel like I'll look back in 6 months or a year at some of the MXML and AS3 that I'm writing and shake my head. The documentation is pretty solid and the Flex community is as generous as the CF community is in sharing learning experiences and code. With that in mind, I'll begin by sharing a little bit of my code from cfcFlexplorer.

To give a little background for those who are not familiar with the project, cfcFlexplorer is a tool for CFC instrospection and testing. Out of the box CF gives us a sweet function for component introspection: getMetaData(). So my initial goal for the project was simple - take a CFC and retrieve the metadata. Of course I decided to get a little fancier later on and add a navigation tree rather than force the user to type in the full component path, but that's beyond the scope of this post. My first tests for this application were performed by writing my component and testing it (ironic, don't you think?). If you're not familiar with the getMetaData function and what it returns for components, try this code snippet on your own server to check it out.

<cffunction name="getCfcMetaData" access="remote" output="false" returntype="struct">
    <cfargument name="pathToCfc" required="true" type="string">
    <cfset var theCfc = createObject("component", pathToCfc)>
    <cfreturn getMetaData(theCfc)/>
</cffunction>

<!--- to test --->
<cfdump var="#getCfcMetaData("path.to.your.favorite.cfc")#">

You'll notice in the dump that the getMetaData function returns a structure. There are several keys in the structure, many containing misc metadata (go figure) about the component (such as the display name, hint, etc). Take particular notice of the functions key - it contains an srray of structures in which each method of that component is represented as a distinct element of that array. Now notice within any of the given components there is likely another array of structs - this one containing an element for each parameter (or argument) within the given method (or function). Seems pretty intimidating, no?

Watch how beautifully Flex and CF play together. For those experts, look away now if this seems obviously noobish. In cfcFlexplorer, there is a simple remoting call which populates the method grid with the functions in the metadata structure. Here's a quick look at the result handler for the remoting call that retrieves the metadata.

private function handleMethodGridResults(event:ResultEvent):void{

methodGrid.dataProvider = new ArrayCollection(event.result.FUNCTIONS);
}

There is a little more to this function - but this is the only part necessary to show for this post. The remoting call passes the metadata struct to this function, which simply sets the methodGrid's dataProvider to the functions array of structs. Note the reference to FUNCTIONS - since CF returns all caps, we have to refer to it as such in our AS. The only other thing going on here is the coercion (or casting) of the array of structs as an ArrayCollection. This is done to take advantage of the ArrayCollection class and it's ability to play nice with easy grid filtering.

So that's it - a simple grid populated with a simple remoting call. But wait - what about the parameters? How can we display those? Well, that's even easier. Since the parameters were also passed back in an array of structs, we can use that column of the methodGrid dataProvider as a dataProvider for the parametersGrid like so:

<mx:DataGrid id="paramGrid" width="100%" height="95%"
    editable="true"
    dataProvider="{methodGrid.selectedItem.PARAMETERS}">

    <mx:columns>
        <mx:DataGridColumn headerText="Name" dataField="NAME" editable="false"/>
        <mx:DataGridColumn headerText="Display Name" dataField="DISPLAYNAME" editable="false"/>
        <mx:DataGridColumn headerText="Required" dataField="REQUIRED" editable="false"/>
        <mx:DataGridColumn headerText="Type" dataField="TYPE" editable="false"/>
        <mx:DataGridColumn headerText="Default" dataField="DEFAULT" editable="false"/>
        <mx:DataGridColumn headerText="Value" dataField="VALUE" editable="true"/>
    </mx:columns>
</mx:DataGrid>

We're simply telling the paramGrid to use the methodGrid's currently selected parameters as the dataProvider.

That just about wraps up my first in depth post on Flex. Hopefully this will benefit others who are just getting started with Flex or inspire someone who is on the fence about trying Flex because it seems a little intimidating. As I said above, the learning curve can be a little difficult to overcome (and I'm still very much learning myself), but trust me - dig into the sample applications and really try to figure out what's going on and you'll see that it will all start making sense very quickly.

Comments (2)

Brian Rinaldi's Gravatar From one Flex noob to another, I highly recommend using Cairngorm. The initial work seems painful, and you feel as though they just took every pattern in the book and implemented it. However, once you grasp the workflow, adding new features is unbelievably easy and the whole application has all of its concerns nicely separated out making it much more maintainable.

I used Cairngorm for the recent Flex version of my code generator...getting the first features in was excruciating, but by the middle of building it I felt comfortable with Cairngorm and the work just cruised :)

Ryan Everhart's Gravatar Hey,
Thanks for putting this together Todd, I might give it a shot!

Ryan