4 Replies Latest reply on Dec 28, 2007 4:07 PM by paulfeuer

    Reference Application object in function

    GeekyCoder
      I know that mxml will eventually compile to actionscript. So how does I reference this class as parameter in function ?

      Assuming the following function is external actionscript file which reference the mxml, so instead of passing the Generic Application class, I want to use the mxml class instead for compile time checking, so how I can I reference it instead of Application parameter ?
      internal function callFunc(app:Application):void
      {

      }





      The following flex file is called tester.mxml

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical"
      paddingTop="0" paddingLeft="0" paddingBottom="0" paddingRight="0" width="100%" height="100%"
      creationComplete="init(event);">

      <mx:Label id="lblHello" text="meme" width="100"/>
      <mx:Spacer height="20"/>
      <mx:Label id="lblTestme" text="hello" width="200"/>

      <mx:Script>
      <![CDATA[

      internal function init(e:Event):void
      {

      }

      internal function callFunc(app:Application):void
      {

      }

      ]]>
      </mx:Script>

      </mx:Application>
        • 1. Re: Reference Application object in function
          paulfeuer
          if the function is in the same mxml document, you can use "this"

          otherwise, look for "document" or "parentDocument" properties

          in Application, you can find it at Application.application.document

          ./paul
          • 2. Re: Reference Application object in function
            paulfeuer Level 1
            actually, i should also point out that if your function is in the mxml document like that, you don't use "this" as a parameter to the function. you can simply refer to this within the body of the function.
            • 3. Re: Reference Application object in function
              GeekyCoder Level 1
              Paul, thank for the response. What I mean is how can I reference a mxml in a external actionscript file.

              For example, I create the following mxml file.
              1) GuiScreen.mxml
              <mx:Application>
              <mx:Label id="lblTest">
              </mx:Application>

              2) myFunc.as = external file that reference the mxml class as parameter for compile-time checking
              package
              {
              public function init(ui:GuiScreen):void
              {
              ui.lblTest.text = "Hello";
              }
              }

              So the above is what I referring to actually. Unfortunately it does not work. So in what ways can I perform the above in Flex's way ?
              • 4. Re: Reference Application object in function
                paulfeuer Level 1
                i haven't really embedded applications like that.

                the reusable bits i have are objects (either .as or mxml) and i wrap them in an application shell.

                i guess i have two questions:
                - who is calling your init( ui:GuiScreen ) method and where are they getting the parameter?

                - you could try adding a script block with the following:
                private static var _instance:GuiScreen = null;
                public static function get instance():GuiScreen {
                if( _instance == null ) _instance = new GuiScreen();
                return _instance;
                }
                override protected createChildren():void{
                super.createChildren();
                if( _instance == null ) _instance = this;
                }

                then anywhere else, you could say:

                var ui:GuiScreen = GuiScreen.instance;


                ./paul