Solved

Flex 4: Accessing public method in main application from component

Posted on 2010-09-13
4
719 Views
Last Modified: 2013-11-11
I need to be able to call a method from a component located under the main application in Flex 4. Can anyone tell me please how to do this without using FlexGlobals please?

Sample code is attached. Thanks in advance.
// TestApp.mxml (application)
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       creationComplete="initApp()">
    <fx:Script>
        <![CDATA[
            import com.TestComp;
            
            import mx.managers.PopUpManager;
            
            public function myMethod():void
            {
                // do something
            }
            
            protected function initApp():void
            {
                var popUp:TestComp = new TestComp();
                
                PopUpManager.addPopUp(popUp, this, true);
            }
        ]]>
    </fx:Script>
</s:WindowedApplication>

// TestComp.mxml (component)
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"
         width="400" height="300">
    <fx:Script>
        <![CDATA[
            private function doSomething(event:MouseEvent):void
            {
                // call to myMethod() in TestApp.mxml
            }
        ]]>
    </fx:Script>
    <s:Button click="doSomething(event)" label="Click Me"/>
</s:Group>

Open in new window

0
Comment
Question by:DReade83
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 500 total points
ID: 33662310
Well you could keep on going up the parent-chain until you have an object of type WindowedApplication. Have a look at the code below (This will not work, because your function is "private" and because it needs a MouseEvent parameter, which I didn't provide). But I would strongly disencourage you to to it that way.

The Flex way would be to make your component dispatch events and to thave your doSomething function be resistered as listener for that events (As your function is allready registered as callback for MouseEvents). For more infos on custom events in Flex, have a look at these links:
http://stackoverflow.com/questions/637841/flex-dispatch-custom-event-on-receipt-of-standard-event
http://blog.calevans.com/2010/02/15/a-flex-3-custom-events-primer/
            private function doSomething(event:MouseEvent):void
            {
                var curParent:Object = this.parent;
                while(!(curParent is WindowedApplication)) {
                    curParent = curParen.parent;
                }
                curParent.doSomething();
            }

Open in new window

0
 

Author Comment

by:DReade83
ID: 33662363
Thanks, but could you provide an example of what you mean in my code snippet please?
0
 

Author Comment

by:DReade83
ID: 33662738
Another thing, what if I need to pass a parameter or two to the method? How do I do that if I'm calling an event?
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33663792
Well I would suggest that you creat a custom event type and add the properties you want to pass to that Event. The callback in the main application component will thenn be able to extract the data and process it.

This event Processing needs a little getting used to, but it allows you to make your applications a lot simpler. In your case you would only have to listen for your custom events in the main application. Any events of that type being fired by any child of that component will automatically be caught.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question