Refreshing a Flex Page

After a good amount of googling,i stumbled upon the below solution to refresh the Flex Page with-out much of a hassle with events or event-listeners.


protected function reloadpage(event:MouseEvent):void
{
  var ref:URLRequest = new URLRequest("javascript:location.reload(true)");
  flash.net.navigateToURL(ref, "_self");
}

Courtesy: Kumar Gandhi

Calling a JS Function from Flex

A simple and elegant of calling a js function is as follows;

a) Include the js function in your Sample.jsp


function testJSFunction()
{
	alert('Test JS Function');
}

b) Include the following piece of code in sample.mxml from where you want to call the JS function


import flash.external.ExternalInterface;
.....
if(ExternalInterface.available){
	ExternalInterface.call('testJSFunction');
}

Note: Assuming that the JSP page is loading the SWF file which loads the Sample.mxml file

Communicating between a JSP and Flex application

As part of Flex development, we had designed our UI in such a way that, there was a Main Action class which used to re-direct to Main.jsp. In this JSP, we had included the SWF file in Object tag which would load the Flex application for us. We had a requirement to pass the user-details to the Flex application from this very JSP rather than making an explicit call from Flex. In addition, we also had a requirement to set this data globally to all the Flex screens. We achieved this by following the below steps:

a) Set the User details in HttpSession or HttpRequest as an attribute or header variable
b) Make use of “FlashVars” attribute in Flash and set the parameters in the Main.jsp in a Java-Script block as shown below:

var userName= '';
var flashvars = 'userName='+userName;

c) Include flashVars while embedding the Flash Content SWF


AC_FL_RunContent(
"src", "MyApplication",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "MyApplicationId",
"quality", "high",
"bgcolor", "#869ca7",
"name", "MyApplicationName",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer",
"FlashVars", flashvars
);

d) Include it in the Object Tag of the JSP page under tag


<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="MyApplicationId" width="100%" height="100%"
			codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
			<param name="movie" value="MyApplication.swf" />
			<param name="quality" value="high" />
			<param name="bgcolor" value="#869ca7" />
			<param name="allowScriptAccess" value="sameDomain" />
			<param name="FlashVars" value="" />
			embed src="MyApplication.swf" quality="high" bgcolor="#869ca7"
				width="100%" height="100%" name="MyApplicationName" align="middle"
				FlashVars=""
				play="true"
				loop="false"
				quality="high"
				allowScriptAccess="sameDomain"
				type="application/x-shockwave-flash"
				pluginspage="http://www.adobe.com/go/getflashplayer"
			embed
</object>

e) Access the ‘FlashVars’ in mxml file as shown below in the Header.mxml:


<s:Label text="{FlexGlobals.topLevelApplication.parameters.userName}, {resourceManager.getString('resources','Welcome')}"/>

f) Finally, dont forget to include the import statement in the Header.mxml

import mx.core.FlexGlobals