![]() ![]() ![]() |
||
|
Generally, it is good coding practice to separate business logic from the display. The ColdFusion page that you just created contains both business logic (the database query) and presentation (the output code block). To separate them, you put the query in a ColdFusion component (CFC). Doing so separates business logic from presentation; it also makes it easy to reuse the query anywhere in your application. For more information, see Building and Using ColdFusion Components in ColdFusion MX Developers Guide.
ColdFusion components (CFCs) are special files saved with the filename extension .cfc. They can contain data and functions. Within CFCs, functions are referred to as methods. Actions that you want ColdFusion to perform, such as querying a database, are contained in component methods. One CFC can contain many methods. Each method in a CFC can return only one variable. The following is the general syntax of a CFC:
<cfcomponent> <cffunction name="firstMethod"> <!--- CFML code for this method goes here. ---> </cffunction> <cffunction name="secondMethod"> <!--- CFML code for this method goes here. ---> </cffunction> </cfcomponent>
Note: If you are using Dreamweaver, select Dynamic page in the Category list, and select ColdFusion Component in the Dynamic Page list.
<cfcomponent displayName="Get Trips" hint="Get trip information"> <cffunction name="basicList" displayName="List all trips" hint="List trips in same order as in table" access="public" returnType="query" output="false"> <cfreturn> </cffunction> </cfcomponent>
Dreamweaver lets you create a ColdFusion component without having to enter the code.
To copy the query to the CFC, you copy the CFML to the CFC, between the opening and closing cffunction
tags.
<cfquery name="TripList" datasource="CompassTravel"> SELECT trips.tripName FROM trips </cfquery>
<cfcomponent displayName="Get Trips" hint="Get trip information"> <cffunction name="basicList" displayName="List all trips" hint="List trips in same order as in table" access="public" returnType="query" output="false"> <cfquery name="TripList" datasource="CompassTravel"> SELECT trips.tripName FROM trips </cfquery> <cfreturn> </cffunction> </cfcomponent>
<cfreturn TripList>
To perform the query that is now in a method in a ColdFusion component, you have to call (invoke) the method. To do so, you can use the cfinvoke
tag. Within the cfinvoke
tag, you specify the name of the ColdFusion component, the method to call, and the query to return to the calling page. The name of the component includes the package, "cfdocs.getting_started.my_app.components." The package looks very similar to the path, except that it contains periods instead of slashes. Like a path, it specifies the location of the component.
<cfinvoke component="cfdocs.getting_started.my_app.components.gettrips" method="basicList" returnvariable="TripList"> </cfinvoke>
Dreamweaver lets you invoke a method in a CFC without having to write the code.
returnvariable
to be the name of the query, TripList, as follows:
returnvariable="TripList"
|
||
![]() ![]() ![]() |