8 Replies Latest reply on Nov 19, 2007 6:24 AM by ntsiii

    How to separate embedded ActionScript from MXML

    Mxmler
      Hi all,

      I need to separate the embedded ActionScript codes located in between <mx:Application> tags (inside MXML). I need to
      do this, becuse it will help me to simplify my job.

      The embedded ActionScript code looks like this:

      <mx:Script>
      <![CDATA[
      private var services:RemoteObject;
      public function initApplication():void
      {
      services = new RemoteObject("GenericDestination");
      services.source="com.mydomain.Services";
      services.authorizeAccessTicket.addEventListener(ResultEvent.RESULT, loginResultHandler);
      }
      .
      .
      .
      ]]>
      </mx:Script>

      Once I taked the whole codes, and placed inside a class called Main.as and refere this class inside <mx:Application>
      as following:

      <mx:Script source="Main.as" />

      I got the following error: 1037: Packages cannot be nested.

      Main class looks like this:
      package
      {
      import mx.rpc.remoting.RemoteObject;

      public class Main
      {
      private var currentUser:User;
      private var currentAccessTicket:AccessTicket;

      private var services:RemoteObject;


      public function Test()
      {
      services = new RemoteObject("GenericDestination");
      services.source="com.mydomain.Services";
      services.addEventListener(FaultEvent.FAULT, faultHandler);
      services.authorizeAccessTicket.addEventListener(ResultEvent.RESULT, loginResultHandler);
      .
      .
      .
      }
      }
      }

      What I have to do in this case?
        • 1. Re: How to separate embedded ActionScript from MXML
          levancho Level 3
          if you do include then you dont need package and clas declarations, jus create some as file name it whatevert you want and put this :

          private var currentUser:User;
          private var currentAccessTicket:AccessTicket;

          private var services:RemoteObject;


          public function Test()
          {
          services = new RemoteObject("GenericDestination");
          services.source="com.mydomain.Services";
          services.addEventListener(FaultEvent.FAULT, faultHandler);
          services.authorizeAccessTicket.addEventListener(ResultEvent.RESULT, loginResultHandler);
          .
          .
          .
          }
          and then use include to uncide that codeanywhere you want in mxml.
          • 2. Re: How to separate embedded ActionScript from MXML
            ntsiii Level 3
            As Levancho says. If you DO want to use a class, then instantiate it first:
            <Main id="main" />

            then in code you can say:
            main.Test()

            Tracy
            • 3. How to separate embedded ActionScript from MXML
              Mxmler Level 1
              The problem here, if I do that then the arrows that could expand and collapse functions will disappear.

              I need to define the package and class declaration for some reasons.

              I have here MXML file called index.mxml and ActionScript called Main.as
              Within script tag inside index.mxml I defined the following:

              <mx:Script>
              <![CDATA[

              import com.mydomain.Main;

              public function initApplication():void
              {
              var main:Main = new Main(this);
              }

              ]]>
              </mx:Script>

              For the custom events that index.mxml was listening (this issue has been solved, Main can listening to them because I passed application instance through Main constructor).

              The problem here, I need to access some component inside index.mxml
              from Main.as by the following code:
              Note: component's name = resultExhibitorWrapperView

              application.getChildByName("resultExhibitorWrapperView").resultExhibitorView.relationshipR esultArray = relationshipResultArray;

              OR

              Note: component's id = resultExhibitorWrapperView

              application.resultExhibitorWrapperView.resultExhibitorView.relationshipResultArray = relationshipResultArray;

              And once I tried I got the following message:

              1119: Access of possibly undefined property resultExhibitorView through a reference with static type flash.display:DisplayObject.

              How can I fix that?
              • 4. Re: How to separate embedded ActionScript from MXML
                levancho Level 3
                what data type are you using in main to store APplication refference?
                looks like its not either Application or Object, it mustbe something else, and thats the problem,
                either user Object (then you loose IDE autoComplete feature) or use datatype Application, or cast it to Application whenever you need to access its children
                • 5. Re: How to separate embedded ActionScript from MXML
                  Mxmler Level 1
                  I am using Application datatype. I got that problem during the compiler time.
                  • 6. Re: How to separate embedded ActionScript from MXML
                    levancho Level 3
                    what s resultExhibitorView? a mxml component with id="resultExhibitorView' or action script instantiated component if so what is the modified for it? public?
                    • 7. How to separate embedded ActionScript from MXML
                      Mxmler Level 1
                      Exactly: resultExhibitorView (a custom component with id = "resultExhibitorView"). I guess there is no way to deal with those components with application datatype unless I treated as index datatype.
                      • 8. Re: How to separate embedded ActionScript from MXML
                        ntsiii Level 3
                        You can explicitly type the application reference variable as "index" (an mxml file becomes a class named the same as the msml file name). then the compiler will find the public methods in it correctly.

                        Alternatively, you can turn off compile-time type checking, and you can type the app variable as Application.

                        Tracy