FlashVar manager using Swiz or RobotLegs
I use FlashVars a lot. Sometimes I need to open or launch something from my application on startup. Sometimes I just need to know some authentication credentials and that’s the way they are passed to me. Something that I have found that has made this easier for me has been to create a FlashVarManager for my projects.
FlashVars are gleaned from a LoaderInfo object and it is just a dynamic Object. (loaderInfo.properties to be exact). But you probably knew that. Being a fan of strongly typedness I usually create some getters on my manager that will fetch a specific flashVar from the .properties object and if there isn’t one give me a default. Since I always did this for Flex projects the most straightforward way to grab what I needed was through the Application instance. My manager would look something like this.
{
public class FlashVarManager
{
private static var:_instance:FlashVarManager;
private var _flashVars:Object;
public static function getInstance():FlashVarManager
{
if(!_instance)
{
_instance = new FlashVarManager();
_flashVars = Application(Application.application).loaderInfo.properties;
}
return _instance;
}
public function get flashVars():Object
{
return _flashVars;
}
public function get specificFlashVarValue():String
{
if(flashVars[‘specificValue’] != undefined)
return flashVars[‘specificValue’];
else
return "someDefaultValue";
}
}
}
Something like that.
So if ever my flashVars had to change I could just change this class. I will often perform logic in here too; sometimes create a faux “flashVar” that is based on a number of flashVars. I find it pretty handy.
But I’m trying to eliminate Singletons wherever I can. Not necessarily Singletons in the “one instance exists” sense, but more in the .getInstance() sense. RobotLegs and Swiz are really helping me out with that. But how to construct my FlashVarManager with those tools? How do you get to the loaderInfo of the app (without using the messy Application() hack)?
Read more
Signals may spell the death to my Commands as I know them
Back when I wrote my Command library I was doing a lot of calling of commands. It actually stems from the Commands/Delegates I created for the Facebook AS3 API (which have since been removed; which I think is a shame). Essentially the greatest benefit I got from my commands with the ability to do this:
function onComplete(commandInstance:CommandClass):void
{ // do stuff }
instead of this:
function onComplete(e:Event):void
{
var commandInstance:CommandClass = e.target as CommandClass;
commandInstance.removeEventListener(Event.COMPLETE, onComplete);
//NOW I can do stuff
}
That got rid of a lot of code for me and made me very happy.
Boss: Jason, how many lines of code did you write today?
Me: (proudly) Negative two-hundred-sixty-nine!
Just kidding. My boss has never asked me how many lines of code I wrote.
Read more