The logical architecture of a Fusebox app resembles a hub and spoke system, with all actions returning to the hub (the Fusebox). This sort of structure is also known as a circuit application.
Figure 1 Fusebox App Structure
A circuit application is usually a single directory of files and generally does a few related tasks such as search. The overall application is called the home application, which is made up of many circuit applications. This is where Fusebox gets its name. Just like an electrical Fusebox, it is set up as a group of circuits ("fuses") that are ready to send the user to whichever part of the application his or her next click requires. Each of these fuses has a name, called a fuseaction. Fuseactions are used to turn on the appropriate switches to cause the required action. So the Fuseaction is the key to the application?without a fuseaction, the application will only do the default fuseaction.
<FORM action="INDEX.CFM" method="post">
<input type="hidden" name="fuseaction" value="search">
</FORM>
|
Okay, so now you are probably wondering about the internal workings of the INDEX.CFM file. As mentioned earlier, it will use CFINCLUDEs to combine files together to create a working application. But how does ColdFusion use the fuseaction to know which files to combine? This is done using CFCASE/CFSWITCH.
The CFCASE/CFSWITCH tags perform a similar function to a CFIF statement with a bunch of CFELSEIFs. But, using CFSWITCH/CFCASE will run much faster than a similar series of CFIF/CFELSEIF. When there are many ELSEIF's, the logic is exactly the same.
For example, here is a logical statement using CFIF/CFELSEIF:
<CFIF my_variable is "DOG">
"Wooof"
<CFELSEIF my_variable is "CAT">
"Meeooow"
<CFELSEIF my_variable is "COW">
"Moooooo"
<CFELSE>
[[Silence]]
</CFIF>
|
Using CFCASE/CFSWITCH, the same statement would be:
<CFSWITCH EXPRESSION="my_variable">
<CFCASE VALUE="DOG">
"Wooof"
</CFCASE>
<CFCASE VALUE="CAT">
"Meeooow"
</CFCASE>
<CFCASE VALUE="COW">
"Moooooo"
</CFCASE>
<CFDEFAULTCASE>
[[silence]]
</CFDEFAULTCASE>
</CFSWITCH>
|
Since CFSWITCH/CFCASE is faster, it is the method used in the Fusebox architecture. INDEX.CFM will contain CFSWITCH/CFCASE to determine what the user wants to do. Think of the INDEX.CFM as basically one big switch statement and each CFCASE contains the information on what to do for a particular fuseaction. Therefore, the EXPRESSION= parameter of the CFSWITCH will be equal to your Fuseaction variable. An example: fuseaction=search might run a search and then display the results. In this case, the opening CFSWITCH statement should assign #FUSEACTION# to be the variable to be used, as in:
<CFSWITCH EXPRESSION="#FUSEACTION#"> |
This means that basically, a fuseaction is the equivalent of a single CFCASE statement in your INDEX.CFM. Have a look at this code:
<CFSWITCH expression="#fuseaction#">
<CFCASE value="Sign_Up_For_Newsletter_Form">
<CFINCLUDE template="/public/Main/blocks/dsp_html_header.cfm">
<CFINCLUDE template="/public/Newsletter/blocks/dsp_registration_form.cfm">
<CFINCLUDE template="/public/Main/blocks/dsp_html_footer.cfm">
</CFCASE>
<CFCASE value="Register_User_For_Newsletter">
<CFINCLUDE template="/public/Newsletter/queries/qry_check_for_dups.cfm">
<CFINCLUDE template="/public/Newsletter/blocks/dsp_registration_thank_you.cfm">
<CFINCLUDE template="/public/Main/blocks/dsp_html_footer.cfm">
</CFCASE>
</CFSWITCH>
|
Suppose a user clicks on the following link:
<a href="INDEX.CFM?fuseaction=Sign_Up_For_Newsletter_Form">Sign up for the newsletter</a> |
Using the example code above, when ColdFusion executes our CFSWITCH using the "Sign_Up_For_Newsletter_Form" fuseaction, it will first display our HTML header block, then it will include the registration form itself and then finally the HTML footer. The rest of the CFCASES will be ignored.
Are you still with me? Great, because now you?ve already got down most of the basics of a Fusebox application. In part III of this article, we will cover some of the finer points of writing a good Fusebox application.