R – AS3/Flex: How to make mxml files loaded via ViewStack see their parent’s variables, etc.

actionscript-3apache-flexflex3oop

For a project I'm working on in Flex I decided to create several separate files for each 'theme' that can be used. Since each theme can and will require specific code, images, styles and virtually anything else, the classical css-only option was not really possible.

I have one problem with this method, and that's that each 'child' mxml file can't read the variables and such created in the parent application. Using Application.application solves half the issues, but any kind of global variable solution seems to fail for me. And the code doesn't get all that much cleaner because of this either.

I created a class that is loaded in the main application with a static variable that I was using as the AS3-equivalent of global variables. Sadly, accessing this from these 'child' mxml files is not possible, I can only re-initiate the class, or write a wrapper function in the main application that fetches these variables. This, again, is anything but perfect and still leaves me with no decent way of using methods from classes that were initiated in the parent application.

What's the best way to get this to work well?

The rundown of the application:

1) Main application loads up several classes/packages, initiates a few with the proper settings and such
2) Main application has a ViewStack which has each theme coming from an external file (themes/ThemeName.mxml)
3) Each theme needs to access at least 2 variables set by the main application (by use of the classes and methods that were loaded), and some may also need to directly access certain features that should be globally available for both the main application, as-well as the specific theme mxml.

I hope my explanation is a bit clear. Please ask any questions that might help you understand this more. Thanks so much in advance!

-Dave

Small update: For a bit more clarrification: I have a class that allows me to easily create a camera view. I initialize and use that class in the main application, which then puts the new (web)cam(era) instance in a variable, ready to be used anywhere where needed. The view file (themes/theTheme.mxml) then displays 2 camera's in whichever way it wants. I want the view file to use the camera(s) created in the main application, so that I won't have to ask the theme creation guys to implement all that over and over. It's one example of why I need this.

Thanks for helping me so far!

Best Answer

Coming from a php background, I see where you might be going wrong. It's not like php where each file is just a big hunk-o-code that can be included into other php files.

Each mxml file is a full fledged Actionscript class. You're not "including those mxml files", you're actually creating properties in your main mxml class, and those properties' types are of the "child" mxml objects.

If your child mxml components really need some information from the parent, you should pass that data into them.

But step back a little bit. Are you sure you want those child mxml files doing all that work? Could they just simply be dumb layouts that don't have any logic in them? Then you could let the main mxml manipulate them.

Related Topic