Integrating Flex Apps with Browsers and Ajax
Day 2. Session 4. Jason Williams shows us two very pertinent pieces of information: A method to send data between Flash player and Ajax, and the sweetness of AMF.
There are some old-school methods for communicating between javascript and actionscript. These generally suck.
The actionscript getURL() function cannot return data, and can only run one simultaneous call at a time (buggy). There is a hacked-together method for communicating from javascript to flash, but this involves loading a 2nd, special SWF with query string params, and it then passes the information along to the actual flash app. Wow these were clunky and really sucked.
So as of flash player 8 there is a new actionscript class called ExternalInterface (flash.external.ExternalInterface) that allows you to expose functions to the browser and javascript. This class has a few functions, 3 of which I’m going to describe:
1) addCallback(publishedName:string, function:reference) - publishes the specified function so it is accessable and invokeable from javascript.
2) call(functionName:string, [ arg1…] ) - call a javascript function from actionscript.
3) available() - returns true if the current browser supports this new interface. Incidentally, the currently supported browsers are: IE PC 5+, Netscape 8+, Mozilla 1.7.5+, Firefox 1.0+, Safari 1.3+.
Apparently it is possible to send complex data like arrays and inline objects as parameters to the function calls in either direction. Strong typing will not be kept though on the Actionscript side.
Of course, there are caveats: 1) re-entrancy: nested calls back and forth are limited to 2 calls in either direction. (4 calls total - #5 will simply not execute) 2) Exceptions are not marshalled. They should be caught inside the native function, and a special case return value.
For Flex, the also created a Flex Ajax bridge - FABridge. Same idea, but geared towards Flex instead of just actionscript.
The last bit is a little more straightforward. Using AMF for downloading data is tens to hundreds of times faster than using flat XML data. Check out a comparison at http://www.jamesward.org/census/.
And lastly Adobe built a FDMS client for javascript, though technically it still uses flash behind the scenes. (Why not just use flash for everything then? Oh well…)
Tags:
nate wrote:
Updated: The actionscript function was renamed above from “isAvailable” to “available” (I checked in the Flash IDE today). Also, added tags, and made it more obvious the caveat refers to “nested calls” only. I’m hoping to use this new ExternalInterface today.
Posted 22 Nov 2006 at 11:02 am ¶