Solved

Flex 4: Accessing public method in main application from component

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to record live broadcast.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now