Over a million developers have joined DZone.

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

DZone 's Guide to

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

· Web Dev Zone ·
Free Resource
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:

<cfcomponent persistent="true" table="T_MENU">
  <cfproperty name="F_COL_NAME" displayname="Column name" type="string" />
  <cfproperty name="F_COL_PARENT" displayname="Column Parent" type="string" />
  <cfproperty name="F_ACTION" displayname="Column Action" type="string" />

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:

<mx:MenuBar id="HomeMenu" labelField="@label" width="100%" height="100%" itemClick="menuItemHandler(event);">

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

var xmlData:XML;
//initialize the XML data
xmlData = <menu>

//iterate over the resultant array and append menuitems to items.
for(var i:int=0;i<event.result.length; i++) {
    //if parent is null then add it to the MenuBar
if(event.result[i].F_COL_PARENT ==null)
xmlData.items.appendChild(<menuitem id={event.result[i].F_COL_NAME} label={event.result[i].F_COL_NAME} parent={event.result[i].F_COL_PARENT} action={event.result[i].F_ACTION}></menuitem>);

//else add the menuitem to its parent
var parentName = event.result[i].F_COL_PARENT;
xmlData.items.menuitem.(@label == parentName).appendChild(<menuitem id={event.result[i].F_MOD_CODE} label={event.result[i].F_COL_NAME} parent={event.result[i].F_COL_PARENT} action={event.result[i].F_ACTION}></menuitem>);
   //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.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}