Shlomi Schwartz
Shlomi Schwartz

Reputation: 8913

Flex - Calling a public method located outside of the main view

New to Flex, I'm trying to figure out a way to call a public method declared on a view which is the "firstView" of the application. How do I get a reference?

Main View

<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160"  firstView="views.loaderView" creationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            private function init():void{
                //how do I access this method?
                        loaderView.setLoaderMsg("My Message");
            }
        ]]>
    </fx:Script>
</s:ViewNavigatorApplication>

Loader View

    <fx:Declarations>
        <s:RadioButtonGroup id="rg"/>
    </fx:Declarations>

    <s:layout>
        <s:VerticalLayout paddingTop="10" paddingBottom="15" paddingLeft="15" paddingRight="15" gap="15" 
                          horizontalAlign="center" verticalAlign="middle"/>
    </s:layout>
    <s:BusyIndicator/>  
    <s:Label id="loaderMsg" text="Loading..." />

</s:View>

Upvotes: 0

Views: 1371

Answers (2)

Astraport
Astraport

Reputation: 1257

Use FlexGlobals.topLevelApplication.myPublicFunction

Upvotes: 1

Florian Salihovic
Florian Salihovic

Reputation: 3961

By default, all children defined in MXML are in the public namespace, so you can easily access it via dot notation. That's the easiest way for the first steps in Flex.

<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                            xmlns:s="library://ns.adobe.com/flex/spark" 
                            applicationDPI="160"
                            firstView="views.loaderView"
                            creationComplete="init()">
  <fx:Declarations>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
  </fx:Declarations>
  <fx:Script>
    <![CDATA[
      private function init():void{
        trace(navigator.activeView);
        // const first:loaderView = navigator.activeView as loaderViewM
        // first.loaderMsg.text = "My Message";
      }
    ]]>
  </fx:Script>
</s:ViewNavigatorApplication>

Upvotes: 3

Related Questions