This content has been marked as final. Show 11 replies
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.
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?
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.
instead of doing
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.
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.
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.
simply make the function public and refer to it using its parent.
am I correct?
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:
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".
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.
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...
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!