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

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.

De-obfuscating javascript code in Chrome Developer Tools

I had blogged about JavaScript debugging with Chrome Developer Tools  some time back, wherein I have explained how these developer tools can help in debugging javascript code. Today Google Chrome 12 was released and my Chrome browser was updated to this version. As with every release, there have been some improvements made on performance, usability etc,. One feature that stood out for me is the ability to De-obfuscate the javascript code. What is Minification? Minification is the process of removing unnecessary characters such as white spaces, comments, new lines from the source code. These otherwise would be added to make the code more readable. Minifying the source code helps in reducing the file size and thereby reducing the time taken to download the file. This is the reason why most of the popular javascript libraries such as jQuery are minified. A minified jQuery file is of 31 KB in size where as an uncompressed one is about 229 KB. Unfortunately, debugging minified javascript f

On GraphQL and building an application using React Apollo

When I visualize building an application, I would think of using React and Redux on the front-end which talks to a set of RESTful services built with Node and Hapi (or Express). However, over a period of time, I've realized that this approach does not scale well when you add new features to the front-end. For example, consider a page that displays user information along with courses that a user has enrolled in. At a later point, you decide to add a section that displays popular book titles that one can view and purchase. If every entity is considered as a microservice then to get data from three different microservices would require three http  requests to be sent by the front-end app. The performance of the app would degrade with the increase in the number of http requests. I read about GraphQL and knew that it is an ideal way of building an app and I need not look forward to anything else. The GraphQL layer can be viewed as a facade which sits on top of your RESTful services o