Creating and Using ColdFusion Components (CFCs)

     

Creating and Using ColdFusion Components (CFCs)

A ColdFusion component consists of one or more functions enclosed in cfcomponent tags. Here's an example of a single-function component named demo.cfc (available on the CD) with a function named demoFunction that returns the text string "This is the result."

 <cfcomponent>     <cffunction name="demoFunction" access="public" returntype="string">         <cfset result = "This is the result.">         <cfreturn result>     </cffunction> </cfcomponent> 

Notice that the function explicitly declares a return type of "string" . ColdFusion throws an error if you try to return a different data type. In addition, the function explicitly returns a value, using the cfreturn tag. It does not use the CALLER scope.

The following code from demo.cfm (also on the CD) invokes the demoFunction function in demo.cfc :

 <cfinvoke component="demo"     method="demoFunction"     returnvariable="result"> ></cfinvoke> 

The variable name used within the component to store the return value and variable name used on the calling page to receive the return value happen to be the same here: "result" . This was done for the programmer's convenience, as a reminder of their relationship. The names could be different. In contrast to custom tags, components don't have to provide the name of the variable that will receive the data on the calling page.

CFCs use the cfargument tag in much the same way that custom tags use cfparam : to define data items that will be passed in. To illustrate this, here are the first few lines of code in scrape.cfc on the CD, a component that implements the same functionality as the custom tag scrape.cfm :

 <cfcomponent>     <cffunction name="scrape" access="public" returntype="array">         <cfargument name="document"  type="string" required="true">         <cfargument name="startRegex" type="string" required="true">         <cfargument name="endRegex" type="string" required="true">         <cfargument name="matchCase"  default="No" type="boolean"> 

From there on in the scrape.cfc component, everything is exactly the same as in scrape.cfm , until it comes time to pass data back to the calling page, in the last line of the function. At that point, scrape.cfc uses cfreturn , like this:

 <cfreturn result> 

Again, unlike the custom tag, the component is not providing the name of the variable to receive the return value. The calling page defines that variable, and only the calling page needs to know its name. The variable name result could be changed in the component to anything else, without requiring any changes in the calling page. Of course, all occurrences of the variable name would have to be changed consistently within the component.



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net