Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 198

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
Invoking methods by using dynamic method names
The
tag is the only way to efficiently invoke different component methods based on variable data (for
cfinvoke
example, form input). In this case, you use a variable name, such as Form.method, as the value of the
In the following example, the user selects a report from a form:
<select name="whichreport">
<option value="all">Complete Report</option>
<option value="salary">Salary Information</option>
</select>
The
tag then invokes the appropriate method, based on what the user selected:
cfinvoke
<cfinvoke component="getdata" method="#form.whichreport#" returnvariable="queryall">
Using components directly in CFScript and CFML
You can invoke methods of a component instance directly using CFScript or in CFML tags. To invoke component
methods directly, use the CreateObject function or cfobject tag to instantiate the component. Thereafter, use the
instance name followed by a period and the method that you are calling to invoke an instance of the method. Always
use parentheses after the method name, even if the method does not take any parameters.
You can use this syntax anywhere that you can use a ColdFusion function, such as in
number signs in the body of a cfoutput tag.
Invoking component methods in CFScript
The following example shows how to invoke component methods in CFScript:
<!--- Instantiate once and reuse the instance.--->
<cfscript>
tellTimeObj=CreateObject("component","tellTime");
WriteOutput("Server's Local Time: " & tellTimeObj.getLocalTime());
WriteOutput("<br> Calculated UTC Time: " & tellTimeObj.getUTCTime());
</cfscript>
In the example, the three CFScript statements do the following:
1
The
function instantiates the tellTime CFC as tellTimeObj.
CreateObject
2
The first WriteOutput function displays text followed by the results returned by the
tellTimeObj instance.
The second
3
WriteOutput
tellTimeObj instance.
In CFScript, you use the method name in standard function syntax, such as
Invoking component methods in CFML
The following example uses CFML tags to produce the same results as the CFScript example:
<cfobject name="tellTimeObj" component="tellTime">
<cfoutput>
Server's Local Time: #tellTimeObj.getLocalTime()#<br>
Calculated UTC Time: #tellTimeObj.getUTCTime()#
</cfoutput>
Accessing component data directly
You can access data in the component's This scope directly in CFScript and cfset assignment statements. For example,
if a user data CFC has a
This.lastUpdated
function displays text followed by the results returned by the
property, you could have code such as the following:
Last updated 1/20/2012
method
tags or surrounded by
cfset
method of the
getLocalTime
method of the
getUTCTime
.
methodName()
193
attribute.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents