Mar 6, 2008

Easy CFC

This is a simple coldfusion pages and the accompanying cfc.  
There are two functions that do different things. It might be
clear as mud so don't hesitate to drop me a line if you get lost,
but here is the idea, I am craeting two functions and invoking
them in differnt ways one example is through createObject and
the other is the cfinvoke tag.

(first our index.cfm page)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>
Untitled Document
</title>
</head>
<body>

<!---
Using cfset to call an object and output a value
In a cfc I have a component that has a function called timer.
Below I an 'instanciating' the object and
assigning it to a variable named 'time'
instead of using cfinvoke I am using the function
createObject. createObject has several parameter settings.
For a CFC and if that CFC is in the same directory as this file.
Then I simply set the createObject to call a component named time.
--->

<cfset time = createObject('component', 'myCfc')>

<!---
Below I am outputting the variable I set above.
Notice I am using the dot notation.
time.timer()
Notice the paranthesis. If my object
took an argument I would place it there.
Like this time.timer(today,tommorow,yesterday,etc)
But I have nothing to pass since
this is only a simple example.
--->

<cfoutput>
#time.x("This is a argument/parameter being passed to my object")#
</cfoutput>
<br<br>

<!---Here is a quick example of a cfc that does some arithmetic.--->

<!--- We are using the invoke method (tag) to pull this object into play --->

<cfinvoke component="myCfc" method="z" returnvariable="goober">

<cfoutput>
#goober#
</cfoutput>

</body>
</html>

(my next file is called myCfc.cfc)

<cfcomponent hint="amigo formats the date...if argument gets passed then i append it to the amigo string">
<cffunction
name="x"
description="the function name is x">
<cfargument
name="today"
type="string"
required="no">

<cfset amigo = timeFormat(now(), "h:MM")>

<cfif isdefined("arguments.today")>
<cfset amigo = arguments.today & " " & amigo>
</cfif>
<cfreturn amigo>
</cffunction>

<cffunction name="z">
<cfset x = 100>
<cfset y = 5>
<cfreturn x/y>
</cffunction>
</cfcomponent>

Labels: , , , , , , , ,