To invoke a method of a component instance using the cfinvoke tag:
Create a file named tellTime2.cfc with the following code:
1
<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:
Create a new ColdFusion page, with the following code and save it in the same directory as the
2
tellTime component:
<!--- Create the component Instance --->
<cfobject component="tellTime2" name="tellTimeComp">
<!--- Invoke the methods --->
<cfinvoke component="#tellTimeComp#" method="getLocalTime"
returnvariable="localTime" >
<cfinvoke component="#tellTimeComp#" 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 modifies and expands the example from the
transiently using the cfinvoke tag"
tellTime component and the
methods. Unlike the CFC example in
getUTCTime
using the cfinvoke
tag", these methods return a value to the caller, and do not display the results
directly; instead, the calling page displays the returned results.
This example is more modular than the previous example because the CFC contains the
functional logic and returns 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.
getLocalTime
section. It uses the
tag to invoke the instance's
cfinvoke
and
getUTCTime
"Invoking component methods
tag to create an instance of the
cfobject
getLocalTime
"Invoking component methods transiently
Using ColdFusion components
.
and
227
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers