Skip to main content

Populating flex MenuBar items in mx:Script using the data retrieved from ColdFusion

Couple of days back I was looking for a piece of code that would populate a Flex MenuBar dynamically. Googled a lot, but I was not able to find the right solution. However, I learnt that a Flex MenuBar can be populated using XML data; but the examples found declared the XML right within the client with all data. What I wanted is to retrieve an array of objects(MenuBar data) from my database and send that to the client. I used ColdFusion ORM to do this for me. It is so nice to see a few lines of code not just retrieve the data and present it very clean; but also save a lot of time.

Let's get started:
On the server side I have two CFC's MenuBar.cfc and MenuBarManager.cfc. 
MenuBar.cfc contains the mapping data:

T_MENU is the table which contains MenuBar data. MenuBarManager.cfc contains a method getMenuBarItems that retrieves all MenuBar data. The output of getMenuBarItems is as shown below:

The above dump shows the Column names, their parent and the action to be taken on the client side i.e. the event to triggered when the MenuItem is clicked. Here File is parent MenuBar item and Exit is its child. The action or method exitEvent should be invoked on the client side on File->Exit .

Now on the client side after retrieving the data from the ColdFusion server, I can iterate over the resultant array of objects and construct the XML data and make that as the dataprovider for my MenuBar.

MenuBar declartion:

The code that would iterate over the array is as below:

var xmlData:XML;
//initialize the XML data
xmlData =


//iterate over the resultant array and append menuitems to items.
for(var i:int=0;i    //if parent is null then add it to the MenuBar
if(event.result[i].F_COL_PARENT ==null)

//else add the menuitem to its parent
var parentName = event.result[i].F_COL_PARENT;
xmlData.items.menuitem.(@label == parentName).appendChild();
   //set the dataprovider property of the MenuBar
HomeMenu.dataProvider = xmlData.items.menuitem;

The above code is self explanatory with inline comments. The last step would be to define the event to be triggered on clicking the File->Exit menuitem. As mentioned earlier the data retrieved from ColdFusion contains a column F_ACTION which declares the event to be triggered (exitEvent). This is identified as an attribute 'action' for each menuitem inserted into the XML data. However, instead of having a very long switch case code I have created the following method/eventHandler which would get invoked on clicking any of the menubar items:

private function menuItemHandler(event:MenuEvent):void {
        if(event.item.@parent != null)
           var functionName:String=event.item.@action;
            //invoke the event

this[functioname](event) would invoke the exitEvent declared in your code.

By the way I use ColdFusion Builder to develop my ColdFusion applications. ColdFusion Builder has strong content assist support which helps the user to build web based applications even more quicker.


  1. awesome simple solution for a problem that took me almost a month... THANK!!!


Post a Comment

Popular posts from this blog

Custom validation messages for HTML5 Input elements using the constraint validation API

HTML5 has introduced several input types such as EMAIL, URL, RANGE, SEARCH, DATE, TIME, etc,. Most of the modern browsers have implemented them and are ready to be used in a HTML document. Another exciting feature introduced in HTML5 is the form validation. Instead of writing JavaScript to validate users input, browsers can now validate it and show an appropriate message if the validation fails. The validation message is shown in line with the field for which the validation has failed. The default error message is shown when the validation fails. In this post I'll explain how these error messages can be changed.

Adding beforeRender and afterRender functions to a Backbone View

I was working on a Backbone application that updated the DOM when a response was received from the server. In a Backbone View, the initialize method would perform some operations and then call the render method to update the view. This worked fine, however there was scenario where in I wanted to perform some tasks before and after rendering the view. This can be considered as firing an event before and after the function had completed its execution. I found a very simple way to do this with Underscore's wrap method.

A cheat sheet of keyboard shortcuts in ColdFusion Builder

In my last post I have explained about keyboard shortcuts in ColdFusion Builder 2.0. This blog post contains a list of all shortcut keys and I have listed these keyboard shortcuts based on the categories that it falls into. Well, this post was not planned since users can easily get to know the keyboard shortcuts by navigating to the preferences (ColdFusion -> Profiles -> Keys). However, I met Joshua at Scotch on the rocks in Edinburgh and he suggested that it would be nice to have the list of keyboard shortcuts handy. So this post is for those who would like to have the list with them and refer it whenever required.

Keyboard shortcuts for inserting text:

These are the keyboard shortcuts which are used to insert some text into the editor:

CommandKeyboard shortcuts on WindowsKeyboard shortcuts on MacInsert anchor tagCTRL + T, LCMD + T, LInsert bold tagCTRL + T, BCMD + T, BInsert br tagCTRL + T, RCMD + T, RInsert cfabortCTRL + T, ACMD + T, AInsert cfdumpCTRL + T, DCMD + T, DInsert cfs…