Wednesday, April 8, 2009

Javascript from Flex

Application calls Javascript from Flex

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

<mx:Script>
<![CDATA[

/* */
private function initApp():void
{
if(ExternalInterface.available)
{
ExternalInterface.call("alert", "Hello from java script");
}
}
]]>
</mx:Script>
<mx:Button label="Call JS" click="initApp()" />
</mx:Application>





AIR window status message

Application to set the status message in AIR window.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="initApp()"
statusBarBackgroundColor="#f0f0f0">
<mx:Script>
<![CDATA[
import mx.core.Application;
/*  */
private function initApp():void
{
Application.application.status = "Staus message goes here";
}
]]>
</mx:Script>
</mx:WindowedApplication>

Application to prevent window close

Application to prevent window close by default and do some action.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.controls.Alert;
/*  */
private function initApp():void
{
this.addEventListener(Event.CLOSING, onClosing);
}
/*  */
private function onClosing( event:Event ):void
{
event.preventDefault();
Alert.yesLabel = "yea";
Alert.noLabel = "na";
Alert.show("You want to exit?","Close", Alert.YES|Alert.NO, this, alertHandler, null, 2);
}
/*  */
private function alertHandler( event:CloseEvent ):void
{
if( event.detail == Alert.YES)
{
this.nativeApplication.exit();
}
}
]]>
</mx:Script>
</mx:WindowedApplication>


Change the yes button label of Alert

Application to change the label of the alert button.

This application is developed using AIR, change the root and end tag to to work as flex application


<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="initApp()">

<mx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.controls.Alert;
/* */
private function initApp():void
{
Alert.yesLabel = "yep";
Alert.noLabel = "nope";
Alert.show("Alert labels customization", "Aler", Alert.YES|Alert.NO, this, null, null, 1);
}
]]>
</mx:Script>
</mx:WindowedApplication>