Select to view content in your preferred language

Accessing an AS3 function in a widget from JavaScript in a containing ASP page

488
2
05-05-2011 01:27 PM
ChrisBeaudette
Frequent Contributor
I'm attempting to access an AS3 function in what is essentially a modified Search widget in the v2.2 Flex Viewer from a JavaScript function on an ASP page that contains the embedded flex viewer.  As a debugging test, I can get at a function in the index.mxml page using ExternalInterface.addCallback() on that page, but that is because the index.swf is loaded in the page using swfobject.js and it has an attribute.id, so I can do a getElementById(attribute.id) in JavaScript to get a handle on the index.swf.  But I'm not sure how to get handle for something like a Search widget that is not loaded directly into the ASP page, but rather is invoked from a toolbar in the index.swf.

Here are some details:

JavaScript in ASP page, invoked by a button on the page:
var flashvars = {};
var params = {};
var attributes = {};
attributes.id = "myMap";
swfobject.embedSWF(mapSwfUrl, "mapDiv", "100%", "98%", mapSwfVersion, xprsSwfInstUrl, flashvars, params, attributes);
. . .
function ClearMapFromAspClearButton() {
   var flash = document.getElementById("myMap");
   flash.AS3ClearMap("clear called from asp");
}


AS3 code in index.mxml:
 <fx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import flash.external.ExternalInterface;
   
   public function initApp():void
   {
    if (ExternalInterface.available) {
     ExternalInterface.addCallback("AS3ClearMap", clear);
     Alert.show("external interface is available");
    }
     
   }
   
   private function clear(msg:String = null):void
   {
    if (msg != null) {
     Alert.show(msg);
    } else {
     Alert.show("no message");
    }
   }
  ]]>
 </fx:Script>


Clicking on the button in the ASP page generates a Flash Alert in the flex map viewer that says "clear called from asp".  This works because I have access to the index.swf object from my calling JavaScript/ASP page.  But how do I get hold of a widget loaded in my map viewer?  Is it necessary to create some kind of bridge from index.swf to the widgets that are invoked "within" it?  Or are widgets somehow accessible as children of the index.swf?  (I didn't see any such relationship when setting a break point and examining the contents of the flash object).
Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus
Chris,

   As I explain in this thread:

http://forums.arcgis.com/threads/13863-Communication-between-widgets

Once you are in the index.mxml code you need to dispatch and event that your widget is listening for is the best way to handle it.
0 Kudos
ChrisBeaudette
Frequent Contributor
Much appreciated, thanks Robert.
0 Kudos