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.

package com.pbking.demo
{
  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

My Swiz Tryout

I’ve just given Swiz a little test-run.  I’m not going to write about how Swiz works or anything here.  There’s plenty of that all over the place by people much more skilled than myself.  But so far my experience has been very positive.  I’ve converted a medium-sized project into using it instead of a hand-rolled solution.  I’m still using a lot of the original code but since it was already in an MVC pattern it was easy to change the important bits.  I’m a big fan of my command library and have a lot of code written that depends on it.  I’m sure there are a dozen just like it out there and probably something just like it rolled into Swiz (I haven’t found out yet, mine worked without change so I didn’t.)  I was very pleased to see that they fit so smoothly into this framework.

Joe Rinehart’s post Swiz in 20 minutes was very instrimental in getting me up to speed very quickly.  I don’t often opt for video presentations over the written work (searching, rewinding, etc is so much harder) but this was so straightforward I was able to watch it once and then just flip open Eclipse and do it.  The minimal documentation on the Swiz page was helpful too of course since my memory only lasts about as long as it takes to switch from one tab to another.

But Swiz is Flex-only.  One of the benefits of Swiz is that it augments the abilities of Flex instead of replaces them.  I’m already using Flex right?  Might as well . . . use it.  But not everything I do is in Flex so I want to see if I can become familiar with a framework that will work in Flash as well and still stay out of my hair.  It’s gotta be “micro” or I just can’t bring myself to care.

Thinking of giving RobotLegs the same treatment.  It’s about to hit 1.0 and that always has a nice sound to it.  Do a quick project conversion to see how it works in a real-world situation.  Maybe Mate; I’ve got peeps that seem to like it a lot.  Any suggestions?