Archive for the 'Quick Tips' Category

Quick Tip: Cairngorm Best Practice

Thursday, January 10th, 2008

When developing Flex applications in Adobe Cairngorm it is quite common to define all event constants as follows:

public static const LOGIN_EVENT:String = "LoginEvent";

The problem with this design is that there is no way of guaranteeing the event type will not collide with an Event type in another package.

Now, I think it is only fair to say that in a good design there typically would not be two Events with the exact same name. However, when developing large scale Flex applications in which additional modules are added with each major release, not to mention the fact that there can be literally hundreds and hundreds of classes, the chances of creating an Event with the same name begins to increase.

As a best practice you should assign the Event type a value which is identical to the Event’s fully qualified class path. This will ensure there can never be a collision of Event types. An example can be seen in the following:

package com.domain.events
{
import com.adobe.cairngorm.events.CairngormEvent;

public class LoginEvent extends CairngormEvent
{    
   public static const LOGIN_EVENT:String="com.domain.events.LoginEvent";

   public function LoginEvent()
   {
       super ( LOGIN_EVENT );
    }
  }
}

It’s always better to be safe than sorry.

Quick Tip: Flex 3 IDE

Saturday, January 5th, 2008

I have added a new Category to my blog called “Quick Tips” from which I plan to post various quick and simple, yet useful little things I come across from time to time.

This first Quick Tip has to do with the default state of the “Mark occurrences feature” in Flex Builder 3 (the feature which causes all methods, properties etc to be highlighted in blue).

Upon installation of the latest Flex 3 beta one of the initial features I noticed was the Mark occurrences feature. Personally I found it to be quite annoying and somewhat intrusive. In addition the feature was also pretty slow (however I believe this is currently being addressed).

In any event I wanted to disable this feature, problem was I couldn’t figure out exactly how to turn it off! After some exploring in the preferences panel I was still unable to find where the Mark occurrences feature could be disabled, so I posted a new topic on the Flex 3 pre-release forum, and luckily I got a quick response from Laurence Mclister explaining where the feature could be disabled - it was right there in the toolbar!

Mark occurrences can be turned off via the toolbar button which looks like a yellow highlighter it is typically toward the center of the toolbar). Below I have highlighted in red where the Mark occurrences feature is located on the toolbar.

Mark Occurrences

Hope that saves someone some time down the road.

ComboBox.prompt

Tuesday, September 11th, 2007

This is one of those little things that you come across from time to time and think to yourself, “man, how did I not come across this before?”.

There has been so many times where I have added an item to a collection in order to display a message to the user. For instance, say you have a ComboBox that displays a list of employees and you want to add a message as the first item, specifying something to the extent of “Select an employee”. Typically, an item would need to be added to the dataProvider of the ComboBox at index 0. Then the issue arises where you will then need to take this item into account when it is selected so that it will be ignored.

Well, like most things in Flex there is an out of the box solution!

The solution is the prompt property which can be utilized to facilitate exactly this sort of thing. I stumbled across this property accidentally while extending ComboBox and viewing the source code. The prompt property simple adds an item to it’s data provider at -1. Since Array indexing begins at zero, this item is ignored and you do not have to take it into account when working with the data provider.

<mx:ComboBox prompt="Select an employee" />

Hope this helps someone down the road.