Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex set function not getting called

Tags:

apache-flex

Okay, I've been banging my head against the wall with this one. I have the following set function in Flex.

    public function set periodChangeAmount(value:int):void
    {
        _PeriodChangeAmount = value;
        refreshStartEndDates();
    }   

If I set the periodChangeAmount to -1 or 1 the set method gets fired. If I set it to zero it doesn't get fired. What's the deal? Does anyone know why it wouldn't get called when setting it to zero. If I change the object type to a number or even an object it still doesn't work as expected. Any help would be greatly appreciated.

like image 768
CodeMonkey Avatar asked Oct 08 '09 16:10

CodeMonkey


1 Answers

Did you put a trace in the setter to make sure it isn't called?

Is periodChangeAmount a Bindable read-write property? In that case flex internally calls the getter to make sure that the value to be set is not the existing value. If the current value of the property (as returned by the getter) is same as the value to be set, the setter is not called.

private var privateVar:Boolean = false;

[Bindable]
public function set readWriteProp(value:Boolean):void
{
    trace("set called with " + value + " current is " + privateVar);
    privateVar = value;
}
public function get readWriteProp():Boolean
{
    trace("get called : " + privateVar);
    return privateVar;
}
//...
a.readWriteProp = true;
a.readWriteProp = true;

Traced output:

get called : false
set called with true current is false
get called : true

Note that the getter was called twice but the setter was called only once. In the second assignment, since the current value and value to be set are same (true), the setter is not called.

I believe flex dev team did it this way to avoid redundancy in binding.

like image 138
Amarghosh Avatar answered Sep 20 '22 01:09

Amarghosh