Solved

Flex 4: Accessing public method in main application from component

Posted on 2010-09-13
4
722 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
[X]
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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
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.
Suggested Courses

732 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