Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build ColdFusion Apps that Last with Mach-II : Page 3

The Mach-II framework has only been around a short time, but it is shaping up to be the most important Web architecture around that supports ColdFusion. Learn how Mach-II can help you build Web apps that are stronger, more organized, and easier to maintain.


advertisement
The Controller
The epicenter of all this activity is the mach-ii.xml file in the config/ directory. This XML script is used to describe the interaction of the four types of Mach-II elements.

First, we define some properties like the name of our application and the default event:

<properties> <property name="applicationRoot" value="/BandNameGenerator" /> <property name="defaultEvent" value="viewSelectBandForm" /> <!--- etc ---> </properties>

In this application, our default event will be the viewSelectBandForm event. Next, we register the listener:


<listeners> <listener name="bandNameGenerator" type= "BandNameGenerator.model.BandNameGenerator"> <invoker type= "MachII.framework.invokers.CFCInvoker_EventArgs"/> </listener> </listeners>

This code registers a CFC called bandNameGenerator and tells Mach-II where the CFC is located. The type attribute of the <listener> references the physical location of the CFC file, which in this case is in the model/ subdirectory of the application.

You may also notice that the <listener> element contains an <invoker> element. Unless you get into advanced uses of Mach-II, don't worry about the <invoker> ; just use the one provided above.

Next we register the <event-handler> for the selectMusicType event from our HTML form above.

<event-handler event="selectMusicType" access="public"> <notify listener="bandNameGenerator" method="generateName" resultkey="request.names" /> <view-page name="viewNames" contentKey="request.content" /> <view-page name="mainLayout" /> </event-handler>

The event-handler notifies the listener we registered above (bandNameGenerator) by calling its generateName() function. Then the event-handler displays two pages: viewNames and mainLayout. Why two pages? I will explain in a moment.

Next, we register our page-views—ColdFusion scripts which display HTML—all of which are located in the view/ subdirectory of the application:

<page-views> <page-view name="viewSelectForm" page="/views/ViewSelect.cfm" /> <page-view name="viewNames" page="/views/ViewNames.cfm" /> <page-view name="mainLayout" page="/views/LayoutMain.cfm" /> </page-views>

There are two main interfaces here: ViewSelect.cfm and ViewNames.cfm. The third interface, LayoutMain.cfm, provides a layout shell within which the content from the other two pages will appear. That's why the event-handler for the selectMusicType event references two page-view elements. The key to how this works is the line from the event-handler element:

<view-page name="viewNames" contentKey="request.content" />

The contentKey attribute asks Mach-II to take the results of running the ColdFusion script ViewNames.cfm and put it in the variable request.content instead of sending it to the Web browser. So all LayoutMain.cfm has to do is display the contents of that variable:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Band Name Generator</title> </head> <body> <h1>Band Name Generator</h1> <cfoutput>#request.content#</cfoutput> </body> </html>

Although an absolutely minimal Mach-II application would not have to use this technique of nesting layouts, it is such a useful and central part of the Mach-II MVC architecture that it is worth using as soon as possible.

And that's it. With all these pieces in place, your Mach-II application is ready to go.

Common Reactions
If you are new to MVC frameworks, it is normal to react with some apprehension to the growing complexity of the mach-ii.xml file. You might find yourself saying: This is a simple program, but it takes all this XML configuration to get it to work. I thought MVC was supposed to simplify my application development!

MVC's big payoff comes not in small applications or in the short term, but in large applications and in the long-term. To see why, think about where the application logic for my sample application would be if it were not in the mach-ii.xml file. Yes, you guessed it: It would be scattered in bits and pieces in the ColdFusion files themselves, via hard coded URLs, and explicit or implicit assumptions about HTTP parameters. What happens when the application grows? That logic grows in kind, and, yes, it is still scattered everywhere. The only way to maintain such an application is with a lot of work.

Explore
There are many other features of Mach-II I don't have space to cover in this article, for example, exception handling, event-mapping, and event-beans. There is also a plug-in API that allows you to extend the framework itself, opening up still more possibilities. The sample application here uses a couple of these to give you a brief taste; the Mach-II site contains many more examples.

The big payoff of using Mach-II, however, will not be found in its bells and whistles, but in its solid delivery of MVC functionality for ColdFusion.



Eric Jansson is assistant director of systems and development for the ACS Technology Center, a regional center supporting the development and use of technology in higher education. His work has focused on commercial and open-source application development in the field of secondary and post-secondary education. Reach him by e-mail .
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap