Flex best practices

31 10 2008

While working on Let’s Go Green application, I learnt some important techniques in Flex that I’d like to share:

1. For rendering a corner radius to a container (for example: Canvas, HBox, etc.), first set the borderStyle to “solid”.

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="200"
backgroundColor="#FFFFFF" borderStyle="solid" cornerRadius="8">

2. For making your layout center and middle aligned in a browser, put all the UI tags inside one container (for example: Canvas, VBox, etc.) and set its horizontalCenter to “-22” and verticalCenter to “-22”.

<mx:VBox id="mainContainer" width="300" height="200" horizontalCenter="-22" verticalCenter="-22">

3. Use weak references for event listeners to make them available for garbage collection. This will reduce memory leaks. Just use the default values for the 3rd and 4th parameters – useCapture and priority – but use true for weakReferences.

addEventListener(ResultEvent.RESULT, onResults_add, false, 0, true);

4. Arrays do not support data binding. In case data needs to be stored in an Array structure and then it needs to be binded with some UI component, use ArrayCollection object which supports data binding.

5. While your application is busy fetching some back-end data, you can set the cursor to let users know that the application is currently busy.

For setting the cursor to busy, use CursorManager.setBusyCursor() 

For resuming the defaultcursor, use CursorManager.removeBusyCursor()