Solved

Flex 4: Accessing public method in main application from component

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
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.

911 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

20 Experts available now in Live!

Get 1:1 Help Now