Flex 2-Style View Source Context Menu for ActionScript 3 Projects

Flex 2 projects have a very neat feature that they get from the mx.core.Application class: A "View Source" option on the context menu.

It's not terribly hard to implement in straight AS3 but it is nice to have it done automatically for you. Unfortunately, ActionScript 3-only projects do not have this by default. As I see the View Source feature as one of the most important features in AS3/Flex 2, I decided to make a simple class that will allow you to do this.

Of course, nothing simple is ever simple. I decided to make it a Singleton (although really it could easily have been a static method). Now, if you need to know one thing about AS3 in its current state: It doesn't like Singletons. By that I mean the combination of rules regarding packages and access specifiers (you can't have a private class in a package any longer) makes it almost impossible to implement a true Singleton... almost! :).

After much head scratching and experimentation, I came up with one method of creating a true singleton in AS3. I'll leave the details of that for another blog post and instead post the SWF here.

My brain's fried -- this is still not a true singleton and you can't really have one in AS3 unless we can have private constructors. In the meanwhile this is just one method of faking it in a convoluted way that offers no protection over accidentally instantiating the class directly via its constructor. Pfft!

If you want the source, just right-click in the AS3 app above and view source ;)

Comments