The Ultimate Resource Manager

How would you like your resousrce manager to be able to access bundle content as easy as RM.bundleName.resourceName no functions used, no parameters, even better with a binding enabled? You like it? Me too. Lets have a look at the implementation.

First we need dynamic variables to be bindable. There are more existing and working solutions for this based on Proxy class. We have to handle getProperty() method and implement IEventDispatcherin order to dispatch events. The final class may looks something like BindableDynamics.

Next, lets do some ResourceManager magic to obtain localized values for our dynamic properties. We do not expect the properties will dispatch changes within one locale, but when locale changes (e.g. switch from en_US to fr_FR) all properties should get updated. A single artificial “propertyChange” event will do a good job for us. Lets call this DynamicResourceManager.

Final step can be custom. For example I like implementation with static (no initialization required) constants (constant works with mxml binding without throwing a warning) that looks something like…

Read the rest of this entry »

Binding in flex 3 magic in curly brackets

A lot of things have been said about binding (read basics). But to really understad what is going on behind the scenes lets do some experiments… First to mention is, the curly brackets magic does not work as BindingUtils does. BindingUtils lets you bind only one setter, but curly brackets lets you bind whole logic.

  • test1 – notice constant C1 in (binding constants is valid, and is working without Binding meta tag) and some plus inside expression “Working: {C1 + ‘ ‘ + v2}”
  • test2 – you can use more curly brackets expressions within one attribute “Working: {C1} {v2.toUpperCase()}”
  • test3 – even if you expect function result from your bindable variable it will work “Working: {C1} {uberFunc(v2)}”
  • test4 – notice some math functions here “Working: {v3 + v3}, {Math.pow(v3, 2)}”
  • test5 – simple Object and Array is not working (nor ArrayCollection using brackets) “Not working: {v4.v}, {v5[0]}, {v6[0]}”
  • test6 – getItemAt() on ArrayCollection works just smooth “Working: {v6.getItemAt(0)}”
  • test7 – once you use curly brackets you can not remove binding, but binding can be unbinded when binded by BindingUtils. (both getter and setter in bindProperty must be public!)
  • test8 – binding works when correct event is dispatched “{foo.gimmeSometing()}”
  • test9 – binding with ternary operator is super powerful “{v2 == ‘world’ ? ‘is world’ : ‘not world’}”

Read the rest of this entry »

Quick tip: invalidating properties on bindables changed

By default, flex bindable properties fires event PropertyChangeEvent.PROPERTY_CHANGE. Sure, those events can be listened thus you can play with them. This can be useful when you have a lot of bindable properties in your classes and you do not want to make getter and setter for each just to be able to catch the moment of update:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" 
    creationComplete="creationCompleteHandler()">
<mx:Script>
<![CDATA[
    import mx.events.PropertyChangeEvent;
    
    [Bindable] private var bVar:uint = 0;
    [Bindable] private var btnLabel:String = "";
    
    private function creationCompleteHandler():void
    {
        addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, propertyChangedHandler);
    }
    
    private function propertyChangedHandler(event:PropertyChangeEvent):void
    {
        if(event.property == "bVar")
            invalidateProperties();
    }
    
    override protected function commitProperties():void
    {
        super.commitProperties();
        btnLabel = bVar.toString();
    }
]]>
</mx:Script>
<mx:Button click="bVar++" label="{btnLabel}"/>
</mx:Application>