Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Flex 4: Accessing public method in main application from component

Posted on 2010-09-13
4
720 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

839 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