views:

62

answers:

2

Hi guys,

I just ran into a strange binding problem. In the mini app below, the Flex Label component is updated when 'someText' changes, but my boundSetter won't be called after the first, initial call.

In short: Why is the boundSetterForSomeText() function not called, while the label does update?

Could anybody please shed some light onto this fundamental issue? Thanks a million!

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="1024" minHeight="768"
    initialize="onInitialize()"
>
    <mx:Panel>
     <mx:Label text="{this.someText}" />
     <mx:Button label="Set random text" click="generateRandom()" /> 
    </mx:Panel>

    <mx:Script>
     <![CDATA[
      import mx.binding.utils.ChangeWatcher;
      import mx.binding.utils.BindingUtils;


      [Bindable(event="xxx")]
      public var someText : String;


      public function onInitialize() : void
      {
       var cw:ChangeWatcher = BindingUtils.bindSetter(boundSetterForSomeText, this, ['someText']);
      }

      public function generateRandom() : void
      {
       this.someText = String( Math.round(Math.random() * 10000) );
       this.dispatchEvent(new Event("xxx"));
      }


      public function boundSetterForSomeText(obj:Object) : void
      {
       trace( obj );
      }
     ]]>
    </mx:Script>
</mx:Application>
A: 

It does work when event is default. (Default event is propertyChange)

[Bindable]
public var someText : String;

I did some debugging and I have no clue why it doesn't work with custom event. I think it should.

zdmytriv
Yeah, weird.. Quite concerning, too. I will post it on Adobe's documentation as a comment.
Tom
A: 

You can use this code to create a get/set pair or "property":

private var _someText:String;

[Bindable(event="xxx")]
public function get someText():String
{
    return _someText;
}

public function set someText(value:String):void
{
    if (_someText != value)
    {
        _someText = value;
        this.dispatchEvent(new Event("xxx"));
    }
}
cliff.meyers
That's right, you don't even need the setter, i.e. you could change the value / dispatch the event in any function you like. But it is critical to have the getter.
Tom