… quite some time now actually (from last Tuesday). In case you don’t know already, the Flex-AJAX Bridge (or the much shorter name FABridge) is a library that allows developers integrate Flex applications with JavaScript code. It is a way to access and use Flex components directly from JavaScript code, without duplicating functionality. It allows accessing objects and functions in the target flash movie on the page, and even registering JavaScript functions as event listeners for flash events.
Getting started …
To make a Flex app available for JavaScript integration via the FABridge you will have to place the FABridge.as file in your project and add a component tag in the application, like this:
<bridge:FABridge bridgeName="flash" id="flash" />
Next make sure you give all of the visual components that you want to access from JavaScript an unique ID (this is something you should do all the time though).
Let’s make it clear, and use an example: we want to have for this demo a Flex component which displays a ‘Hello world’ text and a button which refreshes it. On the JavaScript side we’ll simply have a text box where you can type the message you want displayed in the Flash movie. Also, to showcase event handling, when the refresh button is clicked, a JS alert box will pop up. This is how the end page will look like this:
First step – building the Flex component. It’s quite easy actually:
- To create a simple application just like this fire up your text editor of choice (yeah, you heard me right, no need for Flex Builder here) and paste the following code in:
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="220" height="220"> <bridge:FABridge bridgeName="flash" id="flash" /> <mx:Label x="10" y="25" text="Hello world!" width="157" height="26" fontSize="12" fontFamily="Arial" id="myLabel"/> <mx:Button x="59.5" y="80" label="Refresh" id="myButton" click="myLabel.text='Hello world reloaded!'"/> </mx:Application>
- Now save the new file – let’s say helloworld.mxml.
- Now copy the FABridge.as file from the src folder within the zip you’ve downloaded from here into a folder called bridge on the same level as your mxml file.
- Now open a command prompt and browse to the location of your project. Once there call the mxml compiler – mxmlc.exe. You can find it in the bin folder of your Flex SDK installation.
- At this point you should have a swf file named helloworld.swf in the same folder as your mxml application.
Step 2 – write the HTML page – it’s just as easy:
- Fire up that preferred editor again. Punch in the HTML to create the text box and to load the swf file.
- To enable the web page to interact with the embedded Flex application you will have to add an import for the FABridge.js:
<script src="javascript/FABridge.js"></script>
- To pass the text in the input text box to the flash movie we need to write a JavaScript function – relax, it’s easy, two rows are enough:
function setText() { var flexApp = FABridge.flash.root(); flexApp.getMyLabel().setText(jsMessage.value); }
- Now add the function call to the blur event of the text box and it’s done. Once you type and tab out of the text box, the message will appear inside the Flash movie, just like this:
- For the last touch, let’s register a JavaScript function as an event handler for when the flash button is pressed – this will take some more code, about 4 lines 🙂
function initListener() { var initCallback = function() { var flexApp = FABridge.flash.root(); flexApp.getMyButton().addEventListener("click",textChanged); } FABridge.addInitializationCallback("flash",initCallback); }function textChanged(event) { alert("The text has been reset from Flash"); }
- The textChanged function contains the code which gets executed when the button is pressed. You can do whatever you want in this function. The first function should be called by the body’s onload event and will register an event handler on the Flex button – when clicked, trigger the JS function.
With this simple example, you can see how the FABridge makes Flash – JavaScript communication easier. With just a few lines of code you can control a Flex component without going through the ExternalInterface hassle. You can download the sample application created in this example here.
Now that you know what FABridge does, next post will be about the changes in the latest version. Until then, have fun mixing Flex and AJAX!