9 Replies Latest reply on Jan 9, 2010 9:11 AM by Scott R. Hamilton

    Referencing a control defined in a module from ActionScript

    Scott R. Hamilton

      In a module I have included an ActionScript file using the mx:script directive:

       

      <mx:Script source="../ActionScript/fas_mysql.as"/>

       

      In the script I get an undefined symbol error when I reference

      a control in the module at compile time.

       

      This doesn'tseem right to me. Does the scope of the module not encompass

      the included ActionScript file?

       

      Scott

        • 1. Re: Referencing a control defined in a module from ActionScript
          Flex harUI Adobe Employee

          I would expect that to work.  Try simplifying the test case.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Referencing a control defined in a module from ActionScript
            Scott R. Hamilton Level 1

            I narrowed the problem down. It occurs when two modules
            call the same function in an ActionScript file. The
            function can only reference controls in one of the
            calling modules.

             

            The function attempts to write a value back to a TextArea
            in the calling module. The ActionScript file will not
            compile without errors. The compiler issues a "symbol
            not defined" error for one of the calling module's
            TextArea controls. The TextArea control in the other
            module can be referenced and the value is written back
            successfuly a run time.

             

            Both modules used the same mx directive to include the
            ActionScript file: <mx:Script source="Utility.as"/>

             

            I have exported the simple project developed to show
            this problem and it is attached below.

            • 3. Re: Referencing a control defined in a module from ActionScript
              Scott R. Hamilton Level 1

              Here is a short abstract of the code in case you do not

               

              want to take the time to import the project.

               

              Application: TestModuleScope

               

              Simply loads one or the other of two modules based on
              the user's choice in a radio button.

               

              Each module has a button and a text area. The button
              causes the function WriteToControl in the Utility.as file
              to be called. One parameter is passed in. The function
              writes the value of the parameter back to the text area
              in the calling module.

               

              // ActionScript file Utility.as

               

              private function WriteToControl(sS:String):void {
                  if (sS == "M1") {
                       taM1Text.text = sS;   
                  }
                  else if (sS == "M2") {
                      taM2Text.text = sS;       
                  }
              }

               

              The compiler flags the control taM1Text.text as undefined.
              taM2Text.text can be accessed normally.

               

              Here are the two modules:

               

              Mod1.mxml:

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="576" height="300">
               
                  <mx:Script source="Utility.as"/>
                  <mx:Script>
                      public var sS:String = "M1";
                  </mx:Script>  
                 
                  <mx:Button x="106" y="197" label="Call AS SubRoutine" click="WriteToControl(sS)"/>
                 
                  <mx:TextArea x="315" y="267" height="23" width="25" id="taM1Text"/>
                  <mx:Label x="10" y="268" text="Called ActionScript sub routine writes value here: " enabled="true"/>
                  <mx:Label x="117" y="145" text="Module Mod1" fontSize="14" fontWeight="bold"/>
                 
              </mx:Module>

               


              Mod2.mxml:

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="576" height="300">
                  <mx:Script source="Utility.as"/>
                 
                  <mx:Script>   
                    public var sSS:String = "M2";
                  </mx:Script>   

               

                  <mx:Button x="104.5" y="207" label="Call AS SubRoutine" click="WriteToControl(sSS)"/>
                     
                  <mx:TextArea x="303" y="267" height="23" width="25" id="taM2Text"/> 
                  <mx:Label x="10" y="270" text="Called ActionScript sub routine writes value here: " enabled="true"/>
                  <mx:Label x="116.5" y="147" text="Module Mod2" fontSize="14" fontWeight="bold"/>
                 

               


              </mx:Module>
              • 4. Re: Referencing a control defined in a module from ActionScript
                Flex harUI Adobe Employee

                Unless all variables are defined in both modules, you should get a compile error in each.  I probably won't have time to look at your project any time soon.

                 

                Note that you can use this["taM1Text"].text to get around the compiler caring if a property really is there.

                 

                Alex Harui

                Flex SDK Developer

                Adobe Systems Inc.

                Blog: http://blogs.adobe.com/aharui

                • 5. Re: Referencing a control defined in a module from ActionScript
                  Scott R. Hamilton Level 1

                  Well, the project is really a trivial example but in any case, the abstracted code

                  shows enough to illustrate the problem.

                   

                  I dont't understand your answer though. What do you mean by "all variables must

                  be defined in all modules"? And why can the action script see the control definition

                  in one module but not the other?

                   

                  If I use the syntax you suggest for taM1Text will I be able to write to taM1Text

                  or will I just sneak one by the compiler?

                   

                  Scott

                  • 6. Re: Referencing a control defined in a module from ActionScript
                    Flex harUI Adobe Employee

                    Utility.as expects there to be both a taM1Text and taM2Text property defined in the MXML file.  Maybe if you use the -keep MXMLC option and examine the generated code for the module it will be more apparent.  I have no explanation why it works in module and not the other.  It could be a compiler bug or there is a taM2Text defined in your actual module.

                     

                    Any object's properties can be accessed using obj["someProp"].  The compiler doesn't check but the runtime will throw an error if that property doesn't exist.

                     

                     

                     

                     

                     

                    Alex Harui

                    Flex SDK Developer

                    Adobe Systems Inc.

                    Blog: http://blogs.adobe.com/aharui

                    • 7. Re: Referencing a control defined in a module from ActionScript
                      Scott R. Hamilton Level 1

                      The actual situation is that taM1Text is defined in module


                      mod1 and taM2Text is defined in module mod2.

                       

                      Both mod1 and mod2 include the Utility.as file using the
                      mx:Script directive but Utility.as can only see taM2Text.
                      taM1Text is flagged as undefined at compile time.

                       

                      I cannot imagine this not being a bug.
                      • 8. Re: Referencing a control defined in a module from ActionScript
                        Flex harUI Adobe Employee

                        It certainly is a bug in your code that you are trying to compile code that references variables that do not exist.  If your complaint is that the compiler should have the same error for both modules, please file a bug with your test case and we'll take a look.

                         

                        Alex Harui

                        Flex SDK Developer

                        Adobe Systems Inc.

                        Blog: http://blogs.adobe.com/aharui

                        • 9. Re: Referencing a control defined in a module from ActionScript
                          Scott R. Hamilton Level 1

                          OK, thanks very much for your help Alex, I appreciate it.

                           

                          What I think I will do is created two versions of Utility.as and include

                          each only in one module.

                           

                          Thanks again,

                           

                          Scott