PDA

View Full Version : Overriding set/get enabled -- having issues


DocWatson
09-15-2009, 07:24 PM
I've got a custom component (ApdRow) that overrides the default enabled getters and setters.

[Bindable]
override public function get enabled():Boolean
{
if (this.currentState == "Disabled")
{
return false;
}
return true;
}
override public function set enabled(value:Boolean):void
{
if (value)
{
this.setCurrentState('');
}
else
{
this.setCurrentState("Disabled");
}
}

As you can it SHOULD only set the state to the correct one (default or "Disabled"), and it seems to work perfectly, at least on named instances:

<ns1:ApdRow id="testApd" type="{ApdRowTypes.NUMERIC_STEPPER}" x="10" y="591">
</ns1:ApdRow>
<mx:Button x="10" y="561" label="Disable" click="testApd.enabled = false;"/>
<mx:Button x="86" y="561" label="Enable" click="testApd.enabled = true;"/>

But when I use it on an array that has ApdRows in it (e.g. rowArray[i].enabled = false;), it seems as though the child components in the row take on actual enabled values rather than following the states. Is this an issue with the named instances versus references?

wvxvw
09-15-2009, 08:40 PM
1. Bindable meta is inherited in subclusses, so, no point in putting it there in first place.
2. This is not a way to make bindable setter anyway. You need to specify event type and dispatch that event from a setter or, if the actual assignment is deferred, then dispatch it from the function that actually sets the value.

So, your code should be something like this:
[Bindavle("enabledChange")]
public override function get enabled():Boolean
{
return this.currentState == "Disabled";
}

public override function set enabled(value:Boolean):void
{
this.setCurrentState(value ? "" : "Disabled");
super.dispatchEvent(new Event("enabledChange"))
}
if the enabled property wasn't already bindable on superclass, else, remove the meta and dispatchEvent call.

DocWatson
09-15-2009, 09:06 PM
Just tried your code, wvxvw, and it made everything disabled by default :\

DocWatson
09-15-2009, 09:16 PM
I made some edits to your code and this makes it work the same as before:
[Bindable("enabledChange")]
override public function get enabled():Boolean
{
if (this.currentState == "Disabled")
return false;

return true;
}
override public function set enabled(value:Boolean):void
{
this.setCurrentState(value ? '' : "Disabled");
super.dispatchEvent(new Event("enabledChange"));
}

I've included a picture to show you what is happening. As you can see, the items in the third row are grayed out as though they've been disabled by their default function, when they should both be enabled. The row that says "Not Applicable" and has gray, italicized text is in the proper Disabled state (but was set that way initially).

wvxvw
09-15-2009, 10:49 PM
Just out of curiosity, why did you change this part? :D It really was better before :D
override public function get enabled():Boolean
{
if (this.currentState == "Disabled")
return false;

return true;
}
I really cannot tell from the image what's happening - the best way to tell that is by putting trace() inside the setter though...

EDIT:
Oh, sorry, I didn't know which value you should return if this.currentState == "Disabled".
So, the normal version should be like this:
public override function get enabled():Boolean
{
return this.currentState !== "Disabled";
}

And, I really don't know what Flex conventions say about where should you put access modifier, but, I see no reason to why it should not be the first word in the function definition :) Sorry to mention this...