4 Replies Latest reply on Jul 17, 2009 11:52 AM by Jason San Jose

    example of mxml / external .as file

    Level 2

      can anyone post a simple text posting of an mxml file that calls an external actionscript class file showing both the mxml file and the AS file.

       

      all i get are complaints from the compiler that i have nested packages. can't use <mx:Script>, seems like i have to use <fx:Script source="xxx"> but I can't figure out the nested packages issue.

       

      i am not using [bindable], just straight AS that worked fine when it was embedded in the mxml. of course in the embedded i was not using classes, trying to use classes in the external. a blank package name.

       

      thx.

        • 1. Re: example of mxml / external .as file
          CoreyRLucier Adobe Employee

          The Script tag is a language tag, so in a Flex 4 doc, as you've found, you'll need to use fx:Script to resolve to the MXML 2009 namespace.

           

          So from that point it should work just as it did.  Using the 'source' attribute on the Script tag is the equivalent of 'including' the script file you are referencing.  So the script file should not have any package or class specified, just properties and methods.

           

          So something like:

           

          MyScript.as:

           

          private function foo():void

          {

           

          }

           

          MyMXML.mxml:

           

          <fx:Script include="MyScript.as"/>

          • 2. Re: example of mxml / external .as file
            Jason San Jose Adobe Employee

            Corey is correct here. Including an AS file in an MXML file is the equivalent of adding function and property members to that MXML class. This is something I often got wrong when first learning Flex. i.e. If you have MyComponent.mxml, you really have an AS class called MyComponent.

             

            So, thinking of it that way, you can't "include" a class inside of another class (well, you can, in pure AS where you essentially have an Java-like inner class that sits outside the package declaration for a class file...totally different advanced topic).

             

            If you simply want to use the class in that external .AS file, just add an import statement and make sure it's in your source path.

             

            I hope I understood your question correctly. Let me know if this helps.

             

            Jason San Jose

            Quality Engineer, Flash Builder

            • 3. Re: example of mxml / external .as file
              Level 2

              yes, i understand now. i would say tho that I would prefer to have no source in my mxml. rather, that is for design. i want all my code to be in the .AS files, so in my mind that is where the class is or should be.

               

              I would prefer not to have class Foo because it is 'wrapped' in Foo.mxml.

               

              I want code separation like I have in Flash. Leave my fla for design, do all my coding in .AS (and not in the timeline ! )

               

              but now I understand the Flex mindset, thanks. Adobe programmer support is impressive.

              • 4. Re: example of mxml / external .as file
                Jason San Jose Adobe Employee

                Agreed. Have you investigated the code behind pattern? There are several articles out there, here's one from Adobe devnet http://www.adobe.com/devnet/flex/quickstart/building_components_using_code_behind/.

                 

                Glad I could help.

                 

                Jason San Jose

                Quality Engineer, Flash Builder