When developing Flex or AIR Applications, one of the most common activities is to make data interactive and allow the user to interact with the data in interesting ways. To this end, I often find myself making use of web services, HTTPServices, etc. rather than hard-coding data into my application. I think the usage of web services/http requests, remote objects is common in Flex development but there are a couple pitfalls to watch for when using return data within your components.
I received a question last night from a user who was looking at my post on Grouping Chart Data. After a couple mins looking at his code, I noticed that the majority of it resembled when I had posted except for the <mx:Application> tag. The developer was calling applicationComplete and passing two functions. The first function was responsible for calling the web service, and the second created the chart. He had a datagrid bound to the return value of the web service which was populating yet the chart wasn't showing any data. Here is one of the most common pitfalls when using return data inside your components. When calling the creation of a component (even with databinding) be careful that your component isn't created at the same time as your call to the webservice. I've never had much luck trying to create components this way.
Instead, I would recommend making your call to the component initialization function once you are sure your event.result has returned data. So as an example I would be careful doing this:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="callWebService(), createComponent()">
instead try something like this:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="callWebService()()">
<mx:Script>
<![CDATA[
private function callWebService():void
{
// call your web service and set yourresult event listener to resulthandler
}
private function resultHandler(event:ResultEvent):void
{
// do some processing here and check that the event.result != null
// Call your component initialization function after you have processed your data
createComponent();
}
...