Menu

Life in the Front End: thoughts, trials, tribulations, obscurities…

How to… add FlashVars to Parsley context

It is not uncommon when developing flash/flex application to pass runtime data from the surrounding environment. One very common way of doing this is to define FlashVars properties inside your HTML wrapper and access their values using either the FlexGlobals.topLevelApplication.parameters or LoaderConfig.parameters objects. If you are using an application framework with IOC Container capabilities – in this case Parsley – there is also a good chance that you’d want your FlashVars inside the container/context to be able to inject them in different places.

Below is the sample object which will be passed to the test flex application named ParsleyFlashVars:

The bad: it won’t work. Since parameters is a plain Object, it will never trigger the binding. So the tag will never pick up on the parameters value, trying to add null instead. First the classic binding warning will pop in the console, followed by a nice error message:

warning: unable to bind to property 'parameters' on class 'ParsleyFlashVars'
...
Error: Instance must not be null

The good (again): it is fixable. Simply by wrapping the parameters instance with an ObjectProxy:

The ugly: you end up having a cached variable in the main file just for the sake of firing a binding. The bindable flashVarsProxy property does not really justify it’s existence. One can argue that the overhead is minimal since the RuntimConfig already requires an mxml binding, and perhaps using the word ugly is too much, but it’s definitely not clean.

2. Configuration Properties – e.g. the use of properties, either loaded from external files at runtime or declared inline.
It comes in 3 flavors, but the the interest here is only the PropertyObject tag:

The same good-bad-ugly discussion as above holds here leading to the same conclusion.

Enter Configuration DSL or a more elegant way:

“Added in version 2.3 this configuration option allows to specify most of the features you usually specify with tags (MXML, XML, Metadata) in code with a fluent syntax that allows to add logic to the configuration. The DSL spans multiple levels of configuration, it can be used to create an entire Context from scratch, but it can also be used to create object definitions in code and then combine those with the other configuration mechanisms. Since it is useful for many types of configuration tasks it is now also getting used by nearly all major extension points. Thus when building a custom metadata tag for example, you can use this convenient DSL for adding functionality to an object.”

So for creating the context and specifying specifying configuration class will transform from