Adobe COLDFUSION 9 Manual page 196

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
Invoking CFC methods with the cfinvoke tag
The cfinvoke tag can invoke methods on a CFC instance or invoke CFC methods transiently. You can also use the
tag to invoke CFC methods from within a CFC.
cfinvoke
Invoking methods of a CFC instance
To invoke a component method of a CFC instance, use the
• The CFC instance name, enclosed in number signs (#), in the
• The method name, in the
• Any parameters. For information on passing parameters, see
tag" on page 196.
• If the component method returns a result, the name of the variable for the result in the
The following procedure creates an application that displays the current UTC and local time.
1
Create a file named tellTime2.cfc with the following code:
<cfcomponent>
<cffunction name="getLocalTime" access="remote">
<cfreturn TimeFormat(now())>
</cffunction>
<cffunction name="getUTCTime" access="remote">
<cfscript>
serverTime=now();
utcTime=GetTimeZoneInfo();
utcStruct=structNew();
utcStruct.Hour=DatePart("h", serverTime);
utcStruct.Minute=DatePart("n", serverTime);
utcStruct.Hour=utcStruct.Hour + utcTime.utcHourOffSet;
utcStruct.Minute=utcStruct.Minute + utcTime.utcMinuteOffSet;
if (utcStruct.Minute LT 10) utcStruct.Minute = "0" & utcStruct.Minute;
</cfscript>
<cfreturn utcStruct.Hour & ":" & utcStruct.Minute>
</cffunction>
</cfcomponent>
The example defines two component methods:
2
Create a ColdFusion page, with the following code and save it in the same directory as the tellTime component:
<!--- Create the component instance. --->
<cfobject component="tellTime2" name="tellTimeObj">
<!--- Invoke the methods. --->
<cfinvoke component="#tellTimeObj#" method="getLocalTime" returnvariable="localTime">
<cfinvoke component="#tellTimeObj#" method="getUTCTime" returnvariable="UTCTime">
<!--- Display the results. --->
<h3>Time Display Page</h3>
<cfoutput>
Server's Local Time: #localTime#<br>
Calculated UTC Time: #UTCTime#
</cfoutput>
This example uses the cfobject tag to create an instance of the tellTime component and the
instance's
and
getLocalTime
methods, which return a result to the calling page, and the calling page displays the results. This structure separates the
logic from the display functions, which usually results in more reusable code.
attribute.
method
getLocalTime
methods. In this example, the CFC contains the functional logic in the
getUTCTime
Last updated 8/5/2010
tag and specify the following:
cfinvoke
attribute.
component
"Passing parameters to methods by using the cfinvoke
and
.
getUTCTime
attribute.
returnVariable
tag to invoke the
cfinvoke
191

Advertisement

Table of Contents
loading

Table of Contents