"Logical AND" with flex binding in mxml

While making a small change to a view in one of our flex app's I came across the following little problem.

Initially there was a single parameter binding which set the button enabled, eg:

<mx:Button enabled="{model.user.allowSomething}" label="Do Something">

The business requirements changed and we need to have a global setting as well as a user setting, so now the setting had to be globally enabled and the user had to have access to it.

My 1st attempt was this:

<mx:Button enabled="{model.settings.allowSomething && model.user.allowSomething}" label="Do Something">

However, this gave the following error:

The entity name must immediately follow the '&' in the entity reference.

This you may recognise as an XML formatting error. After a quick google I came across a bug in the adobe bug tracker related to this (BTW I love that this is now open and searchable) which explained the workaround. XML escape the characters - which feels ugly when writing code, but it does make sense.

So the code now becomes:

<mx:Button enabled="{model.settings.allowSomething &amp;&amp; model.user.allowSomething}" label="Do Something">

I also came across another way of handling it using the ternary operator which is very clever but I think it is even less readable than the escaped XML characters.

Cheers, Mark

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.