If you saw some of the junk I have to deal with in our database you’d probably laugh, and if you didn’t it’d because you’re in the same boat as me. I constantly have to deal relational mapping or database compatiblity issues. It seems over the years, whether out of need or some sort of creative insight I apparently lack, my database forefathers have come up with the following list of things that represent a boolean.
True : 1,”A”,”YES”,”Y”,”Active”
False: 0,”I”,”No”,”N”,”Inactive”
Now, of course all those strings are not case sensitive, and are stored as such things as “Active” and “active” depending on which way the wind was blowing when the developer wrote it those many years ago. Almost always I display a boolean value as a checkbox. And so I’d like my checkbox to take all of those values, display it as either checked or unchecked and then spit out a corresponding value when I submit a form. So, if the value went in as “Y” and the user unchecked the box, I should get a “N” when I inspect the value.
1. I extended CheckBox
2. Created a variable called values that will house the two options that get returned
3. Because I want to pass in something other than a boolean, I can’t override the public selected setter found in the mx.controls.checkbox. ( overloaded methods would be nice ). Instead I created a setter called checked that takes type Object, does a switch on the value and sets whether it’s selected or not and also the values array.
4. Finally I create a getter called value from which i can read the correct type. It reads from the values array, where the first index is returned on true else the second index is returned.
Overall, the code is fairly simple and extensible. The only reason I’m blogging about this is because I’d like this kind of functionality out of the box from the flex framework. We do alot of bait n’ switch with data. We support a huge legacy Oracle database and we don’t have much of a choice. Flex is all about the ui, should that include making it easier on the developer to create the ui.
I’d love to see how other people are handling these types of situations. Just as a note, before this I was running my values through a function.
// default to use 1,0
public var values : Array = [1,0];
public function set checked(value:Object):void
{
switch(value)
{
case "Y":
case "N":
this.values = ["Y","N"];
this.selected = (value == "Y") ? this.selected = true : this.selected = false;
break;
case "A":
case "I":
this.values = ["A","I"];
this.selected = (value == "A") ? this.selected = true : this.selected = false;
break;
case "Yes":
case "No":
this.values = ["Y","N"];
this.selected = (value == "Yes") ? this.selected = true : this.selected = false;
break;
case "Active":
case "InActive":
this.values = ["Active","InActive"];
this.selected = (value == "Active") ? this.selected = true : this.selected = false;
break;
case "true":
case "false":
this.values = ["true","false"];
this.selected = (value == "true") ? this.selected = true : this.selected = false;
break;
default:
this.values = [1,0];
this.selected = (value == "1") ? this.selected = true : this.selected = false;
break
}
}
public function get value():*
{
if( this.selected == true)
return values[0];
return values[1];
}
Recent Comments