Search code examples
apache-flexdata-bindingflex4mxml

Unable to bindProperty in flex to specific value


I have a problem with my application in flex4, i have this code for my button:

<buttons:MyButton id="btnNewPost"
    label="{I18nManager.getInstance().main.newPost}"
    minWidth="170"
    click="{doSomething()}"
    creationComplete="btnNewPost_creationCompleteHandler(event)"/>

And on creation complete i have this code:

if (this.newItemButtonEnabledWithCategories)
{
    BindingUtils.bindProperty(this.btnNewPost, "enabled", ModelLocator.getInstance(), "currentCategory");
}

Well, so this is clear, when property currentCategory is not null, then the button will be enabled.

What i want:

I want to dind the property enable to another property of the ModelLocator, itemMode but only if itemMod is ready

I tried BindingUtils.bindProperty(this.btnNewPost, "enabled", ModelLocator.getInstance(), "itemMode"); without luck because if itemMode has any value then the property enabled is always true

Is there any way to only enable item if itemMode is ready?


Solution

  • You might want to use bindSetter rather than bindProperty. This allows you to have a method handle when the property has changed:

    BindingUtils.bindSetter(itemModeChanged, 
                            ModelLocateor.getInstance(), 
                            "itemMode");
    
    protected function itemModeChanged(mode:String):void
    {
        this.btnNewPost.enabled = (mode == "ready");
    }
    

    (As another thought, is there any reason you can't specify the binding in the MXML? enabled="{ModelLocator.getInstance().itemMode == 'ready'} I haven't tried this exact example to verify it works, but I think it should.)