Search code examples
apache-flexactionscript-3data-bindingstaticmxml

React to change on a static property


I'm re-writing an MXML item renderer in pure AS. A problem I can't seem to get past is how to have each item renderer react to a change on a static property on the item renderer class. In the MXML version, I have the following binding set up on the item renderer:

instanceProperty={callInstanceFunction(ItemRenderer.staticProperty)}

What would be the equivalent way of setting this up in AS (using BindingUtils, I assume)?

UPDATE:
So I thought the following wasn't working, but it appears as if Flex is suppressing errors thrown in the instanceFunction, making it appear as if the binding itself is bad.

BindingUtils.bindSetter(instanceFunction, ItemRenderer, "staticProperty");

However, when instanceFunction is called, already initialized variables on the given instance are all null, which was the cause of the errors referenced above. Any ideas why this is?


Solution

  • You have 2 options that I am aware of:

    Option 1 You can dig into the code that the flex compiler builds based on your MXML to see how it handles binding to static properties. There is a compiler directive called -keep-generated-actionscript that will cause generated files to stick around. Sleuthing through these can give you an idea what happens. This option will involve instantiating Binding objects and StaticPropertyWatcher objects.

    Option 2 There is staticEventDispatcher object that gets added at build time to classes containing static variables see this post http://thecomcor.blogspot.com/2008/07/adobe-flex-undocumented-buildin.html. According to the post, this object only gets added based on the presence of static variables and not getter functions.

    Example of Option 2 Say we have a class named MyClassContainingStaticVariable with a static variable named MyStaticVariable and another variable someobject.somearrayproperty that we want to get updated whenever MyStaticVariable changes.

    Class(MyClassContainingStaticVariable).staticEventDispatcher.addEventListener(
    PropertyChangeEvent.PROPERTY_CHANGE,
    function(event:PropertyChangeEvent):void
    {
        if(event.property == "MyStaticVariable")
        {
            someobject.somearrayproperty = event.newValue as Array;
        }
    });