Flex 3 access to main mxml from actioncript code

im writes an ActionScript class to handle web service calls. When I get the result, I want to call the setter method in my main mxml application. My problem is that I don’t know how to access the methods in the ActionScript section of my main mxml class from my actionscript class, any ideas?

+4
source share
3 answers

David is right - as long as you can publicly publish your Application.mxml objects anywhere in your application, the design is a bit of a no-no. It’s better to strive for a free connection between your objects, and how this is done in the Flex idiom usually extends to EventDispatcher and event dispatch. For example, your WebService shell might look something like this:

public class MyWrapperClass extends EventDispatcher { [Event(name="webserviceComplete", type="flash.events.Event")] public function MyWrapperClass(target:IEventDispatcher=null) { super(target); } private function handleWebServiceLoadComplete(event:ResultEvent):void { dispatchEvent(new Event("webserviceComplete")); } public function doWork():void { // Load the service, etc., and ultimately call handleWebServiceLoadComplete()... } } 

... and your Main.mxml file as follows:

 <mx:Script> <![CDATA[ private function app_creationComplete(event:Event):void { var myWrapper:MyWrapperClass = new MyWrapperClass(); myWrapper.addEventListener("webserviceComplete", mywrapper_webServiceComplete, false, 0, true); myWrapper.doWork(); } private function mywrapper_webServiceComplete(event:Event):void { // Do the work you would've otherwise done in the public method } ]]> </mx:Script> 

In this case, the end result is the same - completing the download of the web service calls the function in Main.mxml. But notice how mywrapper_webServiceComplete() declared confidential - it is not called directly by MyWrapperClass . Main.mxml simply signs (with addEventListener() ) to receive MyWrapperClass shutdown notifications, and then does its own work; MyWrapperClass knows nothing about the details of the implementation of Main.mxml, nor does Main.mxml know anything about MyWrapperClass, except that it dispatches the webserviceComplete event and provides the public doWork() method. Free communication and information hiding in action.

Good luck

+7
source

If your class is a UIComponent added to the component tree, you can use its parentApplication attribute. Otherwise, use the static attribute Application.application , but only after application initialization is complete. This field used to be null . Private fields and methods obviously cannot be accessed. Elements declared in the MXML part with an explicit id are publicly available.

Adding such a call creates a tight binding. You might want to consider sending an event and handle this event in the main application.

+6
source

In case someone has the same problem:

mx.core.FlexGlobals.topLevelApplication.YOUR_FUNCTION

is the syntax for accessing public functions in main.mxml.

+1
source

All Articles