Live Google News by SYS-CON!

TOP LINUX LINKS YOU MUST CLICK ON


AJAX, JSON, PHP, and Flex Together
Google has done a great job at using Flash where it makes the most sense, in the graphs for company quotes

Then, we hack around a bit. First of all, we need to convert the data to JSON format:

toJsonString( notifier.data )

We also need to minimize it, because Flex's JSON Library doesn't like new lines or carriage returns in that data (this took me a long time to figure out!). So, we run:

jsmin( toJsonString( notifier.data ) )
Finally, we pas the result of that function to the method in our MXML file, bindJSONToChart():
bindJSONToChart( jsmin( toJsonString( notifier.data ) ) );
I'm lazy and I've made that all one line in the code.

Now, it will do that fine if the .swf file has loaded. However, sometimes the data gets returned before the .swf file is actually loaded. I found this out when running the application locally. So, I've wrapped all this in a try / catch statement, and added an initialization function on the swf file, so that if its not loaded, when it loads it will run this function and populate the graph with data from the Ajax call.

There you go. When you load the HTML page, Spry will load that PHP file using an Ajax call, the PHP file will connect to Feedburner, get the XML data, print it out. Spry will read that in, bind it to the HTML elements on the page, then call the Flex application, passing it the data in JSON format. The Flex application will read in the data and display it in a Flex chart. Here are a few tips and tricks that I've found when building this out:

1.  I found it easiest to modify the HTML that gets output by Flex Builder, rather than to reference the built swf file. If you do that, be sure to modify index.template.html in the html-template folder of your Flex project, rather than the .html files in the bin/ directory of your Flex project. The HTML files in bin/ get overwritten when you save and re-build your Flex application, and if you modify those, rather than index.template.html, you'll lose your changes.

2.  I started building out the Flex graph using JavaScript and the Flex / Ajax bridge. I don't recommend that. Build out your entire Flex component in Flex Builder, then simply write functions to pass data to it from HTML. I think that's easier than trying to build Flex components using JavaScript.

3.  Similarly, write functions that closely couple your Flex application to your HTML page. For instance, you'll notice that in my MXML file, I call the chartClicked JS function using:if( ExternalInterface.available )

ExternalInterface.call("chartClicked", clickEvent.hitData.item);

I could have attached an observer to the lineChart instead, using the Flex / Ajax bridge, but I find it easier to get the data items and debug the application in Flex Builder, rather than trying to do that in JavaScript on the HTML page.

  Subscribe to our RSS feeds now and receive the next article instantly!
In It? Reprint It! Contact advertising(at)sys-con.com to order your reprints!
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS