views:

1713

answers:

4

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!

A: 

Not entirely sure what you're trying to do, but it sounds like you need to use a singleton.

Check out this link for more info http://www.darronschall.com/weblog/2007/11/actionscript-3-singleton-redux.cfm

kenneth
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!
Dave
kenneth
Exactly Kenneth, that's almost the way I went, but instead of using mxml I sticked with Actionscript to 'inject' the camera variables. I might try your way later on, it might be a bit cleaner.
Dave
+1  A: 

Sounds like you're ready for a framework! You can use Dependency Injection to set things in your app as they are created.

Here are some frameworks that help with Dependency Injection.

Note: I am the author of Bifff and Glue.

As an example, using bifff you can say the following (sets the "thePropertyFromMyFile" variable on any component with stylename="customStyleName")

<Selector match=".customStyleName">
  <Set thePropertyFromMyFile="{myGlobalSettings.property}"/>
</Selector>

However, reading your description makes it seem like you are running straight into the need for a full framework for your application. MVC frameworks help you separate the data of your app (in your case, which display mode you are in) from the views, without requiring the views to call Application.application or a global singleton.

This will take quite a bit more research for you, but it is the right path. Any flex app can benefit from a framework, pick the one best for you.

Sean Clark Hess
Although I like the MVC approach, I'm not entirely sure if using a framework on top of Flex is the way to go for me (just yet). So far I'm still getting used to Flex, and so far the code feels anything but clean or comfortable. I'm a PHP guy myself, so that might have something to do with it. Thanks for the heads up, and I've bookmarked them all! I just can't believe there is no (easy) way for me to do this, surely I'm not the only one who has a setup like this :).
Dave
+1  A: 

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.

Marc Hughes
Oh, that sounds strangely logical. I wanted to make the template (child) items as dumb as I could, but for some reason it never occured to me to 'inject' whatever I need from the main application to the client. I was kind-of blind-sighted on getting the client to read from the parent. I'll try this one, sounds very logical. Thanks!
Dave
A: 

you can very easily do this. think of it in OOP fashion. each mxml is a different class. You want to call method of one class in another class. If you want it run in its original scope make the methor variable static and call it by MXMLFileName.MethodName();

If you want to execute in its own scope make an object of the parent MXML and then call the method.

Sandeep Arneja