No _global? No exclude.xml? NO PROBLEM!
_global
It saved your ass on countless occasions. You know what I'm talking about. It helped you get (dirty) work done faster. It encouraged bad coding practices. It got you made fun of by "real" programmers. It was your best friend at 3am when the deadline was 6pm yesterday. And now, in AS3, it's gone.
exclude.xml
It saved your child swfs from compiling classes that were contained in their parents. It optimized your workflow and made working in team development environments easier. And now, in AS3, it's gone.
_global was the "hacky" way to avoid compiling unnecessary classes into child swfs, and exclude.xml was the proper way. How do we reconcile this in AS3 where both have gone bye-bye? The magic of namespaces combined with the Bridge pattern as blogged about by Jesse Warden.
In the AS2 version of my Gaia Framework for Adobe Flash, I have a single static API class that imports many of the largest classes in the framework. To avoid users having to import all those classes, I created a _global reference to that static class. In converting my framework to AS3, I had to find a solution that provided the same functionality. Because of Jesse's aforementioned post, I brought him in on it and here is the solution he hashed out.
First step: create an interface class for every method in my API class. Since the API class is called Gaia, I'll call the interface IGaia. Next, put all the implementation and all the imports that go with it into a singleton class called GaiaImpl, which implements IGaia. Finally, create another singleton class, Gaia, that implements IGaia and has a static var impl:IGaia. But this isn't just any static var, it's a namespace static var.
gaia_internal static var impl:IGaia;
private static var _api:IGaia;
public static function get api():IGaia
{
return _api;
}
gaia_internal static function birth():void
{
if (_api == null) _api = new Gaia();
}
impl is a reference to the GaiaImpl class, and Gaia accesses it as a Proxy for all of the methods in the IGaia interface. Instead of get instance() in Gaia, I use get api(), which is actually less to type and more appropriate here than "interface".
In my main class where all the instantiation happens, the following code takes care of everything.
use namespace gaia_internal; Gaia.birth(); Gaia.impl = GaiaImpl.birth();
birth() is a method I use for efficiently instantiating singleton classes, vs running an if statement inside of the get instance() method. By calling use namespace within a function, it is local to that function and not available to the entire class. Because Gaia.impl uses the gaia_internal namespace, it is able to set it there but impl is not available to any class except Gaia without using the gaia_internal namespace.
What the above allows me to do is import the Gaia class into any swf, and access the static api property, which returns a reference to the singleton instance.
Gaia.api.goto("index/nav/home");
Gaia.api.beforePreload(myFunction);
The swf only imports the interface, which, in my case, is a mere 1.3k. Wowza! That's significantly smaller than the 10k or more the class normally imports. And while it's not the zero k that _global gave me, I have the added benefit of strict-typing and auto-completion in my favorite code editor (FlashDevelop FTW!).
_global and exclude.xml, we had our time together, but I've met somebody else, and while I'll always treasure what we had, I have to let go and move on. Namespaces have stolen my heart.
For more information about this technique, check out Jesse Warden's post about it.
Posted in Actionscript, Gaia, Tips/Tricks

January 22nd, 2008 at 3:08 am
I think the solution I found is better, it involves NO coding and gives you back the intrinsic/exclude.xml functionality! More info here: http://labs.qi-ideas.com/2007/12/25/using-flex-compiled-code-within-flash
September 20th, 2008 at 1:23 am
A really simple solution to bring back _exclude.xml functionality to Flash CS3:
http://www.allflashwebsite.com/article/publish-and-exclude-flash-cs3-plugin-for-optimizing-file-size