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)?

Pretty simple really. Swiz will allow you to mark a controller as an IDispatcherBean. That means that it passes in an IEventDispatcher upon which you can dispatch events into the framework’s event bus. Well that IEventDispatcher just happens to be a DisplayObject from which you can grab the loaderInfo. (The exact item passed in in 0.6.4 and 1.0beta are different but the execution is the same.) Then you just have to include that puppy in your beanmap so that it can be injected wherever it’s needed.

That would look something like this:

package com.pbking.demo
{
  public class FlashVarManager implements IDispatcherBean
  {
    private var _root:DisplayObject;
    public function set dispatcher(newVal:IDispatcher):void
    {
      _root:DisplayObject = DisplayObject(newVal);
    }

    public function get flashVars():Object
    {
      return _root.loaderInfo.parameters;
    }
. . .
 

Doing the same with RobotLegs is even easier. You can have injected into an Actor the “root display” of your context. Pull the loaderInfo out of that and rock and roll. Just map that class into your context and Bob’s your uncle.

package com.pbking.demo
{
  public class FlashVarManager
  {
    [Inject]
    public var contextView:DisplayObjectContainer;

    public function get flashVars():Object
    {
      return contextView.loaderInfo.parameters;
    }
. . .
 

Enjoy!

Filed under ActionScript · Tagged with , ,

Comments

2 Responses to “FlashVar manager using Swiz or RobotLegs”
  1. Owen Corso says:

    do we have to tell the injector to mapSingleton(contextView) in the Context?

  2. Jason Crist says:

    No, the contextView is mapped automatically when you fire up your context. You can inject it wherever it’s needed thusly:

    [Inject]
    public var contextView:DisplayObjectContainer;

Yea but . . .

Tell me what you're thinking.