11 Replies Latest reply on Feb 24, 2007 2:51 PM by WestSide

    How to write global functions?

    WestSide Level 1

      I have some functionality in my app that I feel will be used in multiple places. Typically in HTML/Javascript I could do:

      <script type="Javascript" src="js/functions.js"></script>

      In the functions.js file I could have various functions and call them from any display page.

      How can I have a set of functions like this in Flex/ActionScript? For example I have a couple of places where a user can "Add an Employee" for example. I'd like to call the same function.

      How can I globally include some functions so they can be called from anywhere in my application.

        • 1. Re: How to write global functions?
          michael_ramirez44 Level 1
          I would create a class and just import that classes any place you would like to use it. For utilities you can create a class with static methods.
          • 2. Re: How to write global functions?
            i wonder:

            How can i access public functions or variables inside the script block of main mxml file from other classes?

            i can do in one way:

            var myClassReference:Whatever = new Whatever;

            but how do i do it the other way round?

            • 3. Re: How to write global functions?
              If you are trying to reference variables in your main application tag you can use mx.core.Application.application.<my variable or property>.

              You can also use the parentDocument property to reference the next object up in your Flex application.
              • 4. How to write global functions?
                2 things:

                instead of doing
                functions here...

                make a new actionscript file, code.as or whatever you want to call it.
                then you can do <mx:Script source="code.as"/> and put your actionscript code inside the file like you would inside of CDATA.

                also, SharedObjects are useful for sharing variables between different mxml files.
                they go something like this:
                public var sharedVariables:SharedObject = SharedObject.getLocal("variables");
                (this will need to be decalred in each script)
                to set or retrieve a value you do this:
                sharedVariables.data.myVariable = new String();
                sharedVariables.data.myVariable = 'string here';

                on a further note: SharedObjects are written to the local file system, the user's/client's file system, useful if you need to store keep settings for a user or something like that. Also, getLocal will create the file if it doesn't exsist already, if it does.. it just reads it. Read the docs on SharedObject.

                you will probably find those 2 things useful if you dont already know about them.

                • 5. Re: How to write global functions?
                  WestSide Level 1

                  This is not working for me for some reason.

                  Perhaps I am not phrasing it right, I am trying to have a set of functions that I can call from anywhere.

                  I created a file called functions.as for example. This files resides physically in the root of the site. I am trying to include this file in my "main.mxml" file.

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

                  Then from anywhere in my app, I would like to be able to call those functions. I'd like to just have one instance of this include in my main file and then call these functions from a button in another component for example.

                  • 6. Re: How to write global functions?
                    phi2265 Level 1
                    It looks like you have it correct, are you getting an error? If so, what is the error? One thing to make sure of, if you're calling the .as file in multiple applications, is that you dont reference any of the components by id name in the actionscript file. Make the functions you need return a value when called instead of setting the property of a component.
                    It also looks like there is a space inbetween "functions.as" and /> try removing it.. sometimes spaces in the wrong place throw errors. I don't know if it would error in that case, but try it anyway.

                    • 7. Re: How to write global functions?
                      simply make the function public and refer to it using its parent.

                      am I correct?

                      • 8. Re: How to write global functions?
                        WestSide Level 1

                        When I move the existing function into the "functions.as" file which is included in my "Main.mxml" file I get an error from the view component which contains a datagrid with an "add record" button or whatever. When you click the button it calls a function called addEmployee() for example. Originally I have this function addEmployee() in the view component where the datagrid is and the code works fine, but since this function is going to be called from multiple places, it makes sense to have the code in a more central location so if I change it in one place the changes are propagated.

                        The error I get is "1180: Call to possibly undefined method addEmployee.

                        In my "Main.mxml Application" file I have this code:

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

                        The view component resides in a view stack, in it, there is a datagrid with a button that I want to click and call a function in "functions.as".

                        • 9. Re: How to write global functions?
                          rayess Level 1
                          try refering to the Flex developer giude using the hint "include actionscript".
                          because if u call datagrid.addEmployee() the parser will give an error for sure but if it is a normal method within the actionscript u normally won't get such an error.
                          anyway tell us what happens.

                          • 10. Re: How to write global functions?
                            phi2265 Level 1
                            Are you calling addEmployee() from another mxml component? If so, you need to add <mx:Script source="functions.as"/> in that separate mxml file.

                            Not sure if thats what is happening or not...

                            • 11. Re: How to write global functions?
                              WestSide Level 1

                              I ended up doing what michael_ramirez44 suggested. I created a Class and put static methods in it. I then import the class wherever needed.

                              Thanks for the help here, I tried with the include, but could not get it to work for whatever reason. The include seems to work with the included file in the same directory so perhaps I was having some path issues or issues with referencing functions outside of the calling code.

                              Anyways, the above did the trick!