Intexsoft
Flex: simplifying checks with Data Binding
Dubrovka Eugene

download sources

Let’s assume we have an objects list and a button. The button should show a message box with the name of the object, which is currently selected in the objects list.

With the help of data binding this could be done with the following simple code:

<mx:List id="items" dataProvider="{mynumbers.number}" width="200" />
<mx:Button label="Show name" enabled="{items.selectedItem}"
	click="showItemName(event)" />
...
private function showItemName(event: MouseEvent): void
{
	Alert.show(items.selectedItem.label, 'list item');
}

If items.selectedItem is not null then the expression for the enabled attribute will evaluate to true. Means, a button will automatically react to the selection changes performed in the list and will become available to the user (enabled) only, when something is selected in the list.

As you see, we do not even need to make a check in the showItemName handler function whether an item was selected because the button will not be possible to press until we select something. This is a very simple solution, but it is very intuitive for the user: we can’t do anything until we select something.

And surely this is a much better approach than usage of "nothing happens" logic, when the button is always enabled and after it was pressed we perform a check in the handler, like if (item != null) { /* do something */ }.

 
IntexSoft
(c) 2001-2010 |