Skip to main content
February 8, 2010
Question

Bindable on event

  • February 8, 2010
  • 1 reply
  • 508 views

Hi,

I'm trying to get a timer event to update an  internal variable in a class accessible through bindable getter, but  it's not working. The timer event fires, I can see this in debug mode  but apparently the change propagation through binding on the event  doesn't work. I've been staring at this for half an hour and would  really like to know what I'm doing wrong, if you have an idea please let  me know.

This is my class:

package managers
{
     import flash.events.Event;
     import flash.events.EventDispatcher;
     import flash.events.TimerEvent;
     import flash.utils.Timer;
    
     [Bindable]
     public class TestEventBinding extends EventDispatcher
     {
         public static const MYEVENT:String = "myevent";
        
         public function TestEventBinding()
         {
             super();
            
             refreshTimer.addEventListener(TimerEvent.TIMER,  refreshTimerHandler);
             refreshTimer.start();
         }
        
         private function refreshTimerHandler(event:TimerEvent):void {
             //myNum++;
             myNum = myNum + 1;
         }
        
         [Bindable(event=MYEVENT)]
         public function get myNum():int {
             return _myNum;
         }


         [Bindable(event=MYEVENT)]
         public function set myNum(num:int):void {
             _myNum = num;
             dispatchEvent(new Event(MYEVENT));
         }


        private var  _myNum:int = 0;
        
         private var refreshTimer:Timer = new Timer(1000);
     }
}

And this sample application mxml:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
>
     <mx:Script>
         <![CDATA[
             import managers.TestEventBinding;
       
             private var test:TestEventBinding = new TestEventBinding();
       
         ]]>
     </mx:Script>


    <mx:NumericStepper  minimum="0" maximum="1000" value="{test.myNum}"/>

/>

I  get the warning: assignment to test cannot be detected, which I don't  understand, I have a getter and setter but on test.myNum. For now I don't want to bind  for assignment, just getting the value and update the control based on some  event which is internal to the class (in this example a timerevent).

Anybody  that can see what I do wrong I would be very grateful for the solution.

Thanks  alot

Frederik

This topic has been closed for replies.

1 reply

February 8, 2010

Poster of the question here.

Apparently if I change my class to this it works:

package managers
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
   
    public class TestEventBinding extends EventDispatcher
    {
        public function TestEventBinding()
        {
            super();
           
            refreshTimer.addEventListener(TimerEvent.TIMER, refreshTimerHandler);
            refreshTimer.start();
        }
       
        private function refreshTimerHandler(event:TimerEvent):void {
            myNum++;
        }
       
        [Bindable(event="change")]
        public function get myNum():int {
            return _myNum;
        }

        [Bindable(event="change")]
        public function set myNum(num:int):void {
            _myNum = num;
            dispatchEvent(new Event("change"));
        }

        public var _myNum:int = 0;
       
        private var refreshTimer:Timer = new Timer(1000);
    }
}

Please if you can, could you answer me these questions:

1) the event string has to be "change", I cannot choose my own string?

2) substituing [Bindable(event="change")] for [Bindable(event=MYEVENT)] where MYEVENT is a static const String containing the same text ("change") has a different effect?? (ie. not working)

I'm new to flex and trying to understand the event system better, this is most probably something stupid which I simply cannot see for lack of experience in flex. So any comment would be much appreciated.

greetings

Frederik