228
Chapter 11: Building and Using ColdFusion Components
Using cfinvoke within the CFC definition
You can use the
cfinvoke
tag to invoke a component method within the component definition;
for example, to call a utility method that provides a service to other methods in the component.
To use
cfinvoke
in this instance, do not create an instance or specify the component name in the
cfinvoke
tag, as the following example shows:
<cfinvoke method="getLocalTime">
Passing parameters to methods using cfinvoke
When you use the
cfinvoke
tag, ColdFusion MX provides several methods for passing
parameters to CFC methods:
•
As
cfinvoke
tag attributes, in name="value" format
•
In the
cfinvoke
tag
argumentcollection
attribute
•
In the
cfinvoke
tag body, using
cfinvokeargument
tag
You can use any combination of these methods in a single invocation. If you use the same name in
two or three of these methods, ColdFusion uses the value based on the following order of
precedence:
1
cfinvokeargument
tags
2
cfinvoke
attribute name-value pairs
3
argumentcollection
arguments
Passing parameters using attribute format
You can pass parameters in the
cfinvoke
tag as tag attribute name-value pairs, as the following
example shows:
<cfinvoke component="authQuery" method="getAuthSecure"
lastName="#session.username#" password="#url.password#">
In the example, the parameters are passed as the
lastName
and
password
attributes.
Note:
The
cfinvoke
tag attribute names are reserved and cannot be used for parameter names. The
reserved attribute names are:
component
,
method
,
argumentCollection
, and
returnVariable
. For more
information, see
CFML Reference
.
Passing parameters in the argumentCollection attribute
If you save attributes to a structure, you can pass the structure directly using the
cfinvoke
tag’s
argumentCollection
attribute. This technique is useful if an existing structure or scope (such as
the Forms scope) contains values that you want to pass to a CFC as parameters, and for using
conditional or looping code to create parameters.
When you pass an argumentCollection structure, each structure key is the name of a parameter
inside the structure.
The following example passes the Form scope to the
addUser
method of the UserDataCFC. In
the method, each form field name is a parameter name. Then the method can use the contents of
the form fields to add a user to a database.
<cfinvoke component="UserDataCFC" method="addUser"
argumentCollection="#Form#">
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......