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)
xmlData.items.appendChild();

//else add the menuitem to its parent
     else{
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[functionName](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.

Comments

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

    ReplyDelete

Post a Comment

Popular posts from this blog

File upload and Progress events with HTML5 XmlHttpRequest Level 2

The XmlHttpRequest Level 2 specification adds several enhancements to the XmlHttpRequest object. Last week I had blogged about cross-origin-requests and how it is different from Flash\Silverlight's approach .  With Level 2 specification one can upload the file to the server by passing the file object to the send method. In this post I'll try to explore uploading file using XmlHttpRequest 2 in conjunction with the progress events. I'll also provide a description on the new HTML5 tag -  progress which can be updated while the file is being uploaded to the server. And of course, some ColdFusion code that will show how the file is accepted and stored on the server directory.

Server sent events with HTML5 and ColdFusion

There are several ways to interact with the server apart from the traditional request\response and refresh all protocol. They are polling, long polling, Ajax and Websockets ( pusherapp ). Of all these Ajax and Websockets have been very popular. There is another way to interact with the server such that the server can send notifications to the client using Server Sent Events (SSE) . SSE is a part of HTML5 spec:  http://dev.w3.org/html5/eventsource/

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.