9 Replies Latest reply on Oct 18, 2009 11:38 PM by jake_flex

    Collecting all functions into one file

    RedOctober57 Level 1

      Basic programming ettiquite says that I should put all the ActionScript functions that will be used by different "pages" in my application into a single file that will be visible to all.  Within my project "src" file, I have created a folder called "components", into which I was thinking of putting my action script file that will hold all "general" functions. I then right clicked on the "components" folder and clicked "New/ActionScript File".

       

      Hmmmm... now I'm stuck..

       

      -Is this the correct way to accomplish this?

      -Do I need to put

          <fx:Script>
              <![CDATA[

        at the top of the file?

       

      -How do I make the functions in this file "visible" to all my other .mxml files? Do I "import" it along with the other "import" commands?

      -Is there an example of this some where?

        • 1. Re: Collecting all functions into one file
          jake_flex Level 2

          What you need is a class. If you're using ActionScript, you don't need to use any of the mxml tags. For example, create a file called MyFunctions.as add type the following text in

           

          package components
          {
               public class MyFunctions
               {
                    public function function1() : void
                    {
                         // Add logic here
                    }
               }
          }
          

           

          You'll need to import this class from wherever you wish to use it. Like this

           

          import components.MyFunctions;
          // or import components.*;
          

          To use the functions of that class, you'll need to make an instance (object) of it. Something like this

           

          var mf:MyFunctions = new MyFunctions();
          mf.function1();
          

           

          This should get you started.

           

          As a general notice, having an instance clas called MyFunctions is not very OOP. If the class contains only general util functions, it would be preferable to make its functions static. I recommend you to read about OOP (Object Oriented Programming).

          • 2. Re: Collecting all functions into one file
            RedOctober57 Level 1

            Can't get started.. this is driving me crazy.  I have followed your instructions in the last email to the letter.

             

            When I get to the part where I'm supposed to import, I type:

             

                        import components.MyFunctions;

             

            as per your instructions.

             

            I then save the file and an error shows up:

             

            1172: Definition components:MyFunctions could not be found.    baa.mxml    baa/src    line 136    Flex Problem

             

            I can't get beyond this point.

             

            Suspecting I was missing something, I tried adding the following line to the Application code at the top of the file:

             

            xmlns:components="components"

             

            So it looks like this:

             

            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                         xmlns:s="library://ns.adobe.com/flex/spark"
                                         xmlns:mx="library://ns.adobe.com/flex/halo"                            
                                         xmlns:components="components"

             

             

            But the exact same error occurs.  Can you point out the (probably) simple little thing that I am doing wrong?  Thanks.

             

            (Note:  I also tried import components.*   Same result... no go)

            • 3. Re: Collecting all functions into one file
              PeakDigital Level 1

              Is your line " import components.MyFunctions;" within Script tags in your .mxml file?

              • 4. Re: Collecting all functions into one file
                RedOctober57 Level 1

                yes.  Made sure of that.  Here's the error message I got just now.

                 

                Description    Resource    Path    Location    Type
                1172: Definition components:MyFunctions could not be found.    baa.mxml    baa/src    line 140    Flex Problem

                 

                 

                And I have the line:

                 

                xmlns:components="components"

                 

                up in the Application object tag, as shown here:

                 

                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                             xmlns:s="library://ns.adobe.com/flex/spark"
                                             xmlns:mx="library://ns.adobe.com/flex/halo"
                                             xmlns:components="components"

                • 5. Re: Collecting all functions into one file
                  jake_flex Level 2

                  RedOctober57 wrote:

                   

                  yes.  Made sure of that.  Here's the error message I got just now.

                   

                  Description    Resource    Path    Location    Type
                  1172: Definition components:MyFunctions could not be found.    baa.mxml    baa/src    line 140    Flex Problem

                   

                   

                  And I have the line:

                   

                  xmlns:components="components"

                   

                  up in the Application object tag, as shown here:

                   

                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                               xmlns:s="library://ns.adobe.com/flex/spark"
                                               xmlns:mx="library://ns.adobe.com/flex/halo"
                                               xmlns:components="components"

                  When using mxml tags, to import the class, you'll have to use either

                  xmlns:components="components.*"
                  

                  or

                  mxlns:components="components.MyFunctions"
                  

                  and then define the class as mxml tag

                  <components:MyFunctions />
                  

                  If you're using static functions, you can call the function from your code by the class name

                  MyFunctions.myFunction();
                  

                  and when using instance objects, you'll need add the id attribute to the tag and use that to call your function.

                  <components:MyFunctions id="mf" />
                  
                  mf.myFunction();
                  

                   

                  You can also add the namespace with import statement inside your <mx:Script> block

                  <mx:Script>
                       <![CDATA[
                            import components.MyFunctions;
                            //import components.*;     // Works as well
                  
                            // Then you call the function as mention above with a static
                            // reference or with an instance, depending how you programmed
                            // your class to operate.
                       ]]>
                  </mx:Script>
                  

                   

                  In addition, make sure that the file structure is correct. In your case, components is its own namespace and should have its own directory under your src directory. Probably something like this

                   

                  src
                  > baa.mxml
                  > components
                  >> MyFunctions.as
                  
                  • 6. Re: Collecting all functions into one file
                    RedOctober57 Level 1

                    Hi Jake,

                     

                    Your answer is detailed but I appologize the I don't understand the jargon.  This is my very first attempt at using FlashBuilder and ActionScript.  I'm still having trouble making this work, athought I know to you it must seem very simple.  Here is what I'm having trouble with:

                     

                     

                    xmlns:components=components.*

                    I'm assuming this goes in the <Application> tag... like this...with double quotes....

                     

                    <Application xmlns:components="components.*"    ... (rest of the tag) ...>

                     

                    and then define the class as mxml tag        (<--- What does this mean?  Or, more to the point, can you show me *where* in the code I'm supposed to put this:

                    <components:MyFunctions />

                     

                    Were ever I put this in the sorce code.. inside a tag, in an ActionSript function, in the Declarations tag, FlashBuilder always throws the error:

                     

                    Could not resolve <components:MyFunctions> to a component implementation.    baa.mxml    baa/src    line 350    Flex Problem

                     

                    Next, what is the difference between a static function?  You keep using that term, but I don't know what it means?  How can I tell if I've created or are using a "static function"?

                     

                    Also:

                     

                    and when using instance objects, you'll need add the id attribute to the tag and use that to call your function.

                    <components:MyFunctions id="mf" />
                    mf.myFunction();

                    Can you give me a codes snippet of "using an instance object".  I think I know what that means but, since I can't get past the problems above, I'm not sure.  Can you give me a code snippet?

                     

                     

                     

                    Note:  I'm using the exact code that you stated in the first response to me, so that I have the best chance of understanding.  In the code help hints, I can get a list of options when I type components.  ...which will disply "MyFunctions"... but  I can *never* get past "MyFunctions"... meaning that "function1()" is never listed, and if I just type it in, I get the error message:

                     

                    1120: Access of undefined property MyFunctions.    baa.mxml    baa/src    line 165    Flex Problem

                     

                    Now, everything in your explanation beyond this point looks like an "alternative" way of doing this.  I can't get that way to work either.  Would you be so kind as to explain where I'm going wrong by answering the above questions, and if I can get one way to work, I won't worry about the other different ways of doing it  (for now).

                     

                    Thanks Jake.

                    • 7. Re: Collecting all functions into one file
                      Subeesh Arakkan Level 4

                      In the script tag put the source .Something like this

                       

                      <fx:Script source="MyFunctions.as" />

                      • 8. Re: Collecting all functions into one file
                        jake_flex Level 2

                        OK, here's a complete example

                         

                        First the mxml code, this the the 'main' class. Save this on the root of your src directory. The attribute xmlns imports the namespace util to the main application scope. The tag <ns:1MyUtilClass> takes the class into use, no id required, since the funcion is static.

                         

                        <?xml version="1.0" encoding="utf-8"?>
                        <!-- ClassText.mxml -->
                        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:ns1="util.*"
                            layout="absolute">
                            <ns1:MyUtilClass />
                            
                            <mx:Script>
                                <![CDATA[
                                    import mx.controls.Alert;
                                    
                                    private function handler(event:Event) : void
                                    {
                                        var msg:String = MyUtilClass.myUsefulfunction(); 
                                        Alert.show(msg);
                                    }
                                ]]>
                            </mx:Script>
                            
                            <mx:Button click="handler(event)"/>
                        </mx:Application>
                        

                         

                        And the util AS class. Save it to src/util directory.

                         

                        package util
                        {
                            public class MyUtilClass
                            {
                                public static function myUsefulfunction() : String
                                {
                                    return "hello";
                                }
                            }
                        }
                        

                        Compile and run.

                         

                        RedOctober57 wrote:

                         

                        Hi Jake,

                         

                        Your answer is detailed but I appologize the I don't understand the jargon.  This is my very first attempt at using FlashBuilder and ActionScript.  I'm still having trouble making this work, athought I know to you it must seem very simple.  Here is what I'm having trouble with:

                         

                        Didn't mean to confuse you with my 'jargon'. Like I said in my previous post, I recommend you to read books/tutorials on object oriented programming/design.

                         

                        For your first try at this, it does not really matter.

                        Next, what is the difference between a static function?  You keep using that term, but I don't know what it means?  How can I tell if I've created or are using a "static function"?

                        You define a static function by using static keyword when declaring the function. See the example AS code I posted. When using a static function, you don't need to create an instance of the class in order to call the function. Like in the code exampe, the function is called as MyUtilClass.myUsefulFunction().

                         

                        Also:

                         

                        and when using instance objects, you'll need add the id attribute to the tag and use that to call your function.

                        <components:MyFunctions id="mf" />
                        mf.myFunction();

                        Can you give me a codes snippet of "using an instance object".  I think I know what that means but, since I can't get past the problems above, I'm not sure.  Can you give me a code snippet?

                         

                        The above snippet is an example of using an instance object. Variable mf is instance of class MyFunctions. The other option is to create the instace in AS: var mf:MyFunctions = new MyFunctions. Note that this code snippet is not part of the example posted at top of this post.

                         

                        Whether to use static function or instance functions depends on the purpose of the class you are creating. If it's a collection of functions, like the thread topic says, then go with static functions. Especially if the functions are not related to each other directly.

                         

                        On the other hand, if you can define an independent unit in your code that you would like use as an object, then use instance class.

                         

                        I hope this doesn't helps and doesn't confuse you more.

                        • 9. Re: Collecting all functions into one file
                          jake_flex Level 2

                          Subeesh Arakkan wrote:

                           

                          In the script tag put the source .Something like this

                           

                          <fx:Script source="MyFunctions.as" />

                          This will include the AS code to the mxml code. Didn't know about this before myself. Whether to use include or import is a matter design, and of course of taste. Again, depends on what you want to achieve.

                           

                          The following link provides information between the two.

                           

                          Comparing, including, and importing ActionScript code