Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 24, 2014 23:47
Show Gist options
  • Save bennadel/9751896 to your computer and use it in GitHub Desktop.
Save bennadel/9751896 to your computer and use it in GitHub Desktop.
Creating ColdFusion Components In Parent Directories (From Sub Directories) Without Mapped Paths
<cffunction
name="CreateCFC"
access="public"
returntype="any"
output="false"
hint="Creates a CFC Creation proxy. Does NOT initialize the component, only creates it.">
<!--- Define arguments. --->
<cfargument name="Path" type="string" required="true" />
<!--- Return the created component. --->
<cfreturn
CreateObject(
"component",
("www_cfc." & ARGUMENTS.Path)
)
/>
</cffunction>
<cfcomponent
displayname="Application"
output="true"
hint="I do pre-page processing for the application">
<!---
Run the pseudo constructor to set up default
data structures.
--->
<cfscript>
// Set up the application.
THIS.Name = "CFC_TEST";
THIS.ApplicationTimeout = CreateTimeSpan( 0, 0, 5, 0 );
THIS.SessionManagement = false;
THIS.SetClientCookies = false;
</cfscript>
<!--- Include the CFC creation proxy. --->
<cfinclude template="../create_cfc.udf.cfm" />
<cffunction
name="OnRequestStart"
access="public"
returntype="boolean"
output="true"
hint="I do pre-page processing for the page request.">
<!---
Store the CreateCFC method in the application
scope. We *wouldn't* do this for every page
request... this is JUST an example.
--->
<cfset APPLICATION.CreateCFC = THIS.CreateCFC />
<cfreturn true />
</cffunction>
</cfcomponent>
<cfcomponent
displayname="Girl"
extends="AbstractBaseComponent"
output="no"
hint="I am a girl object.">
<!--- Run the pseudo constructor to set up default data structures. --->
<cfscript>
// Set up default public properties.
THIS.FirstName = "";
THIS.LastName = "";
THIS.ValidPickupLines = "";
</cfscript>
<cffunction name="Init" access="public" returntype="Girl" output="false"
hint="Returns an initialized girl instance.">
<!--- Define arguments. --->
<cfargument name="FirstName" type="string" required="false" default="" />
<cfargument name="LastName" type="string" required="false" default="" />
<cfargument name="PickupLines" type="array" required="false" default="#ArrayNew( 1 )#" />
<!--- Store arguments. --->
<cfset THIS.FirstName = ARGUMENTS.FirstName />
<cfset THIS.LastName = ARGUMENTS.LastName />
<cfset THIS.ValidPickupLines = ARGUMENTS.PickupLines />
<!--- Return This reference. --->
<cfreturn THIS />
</cffunction>
<cffunction name="TryPickupLine" access="public" returntype="string" output="false"
hint="This tries a pickup line on the Girl.">
<!--- Define arguments. --->
<cfargument name="Line" type="string" required="true" />
<!--- Check to see if the pickup line is valid. --->
<cfif (THIS.ValidPickupLines.IndexOf( JavaCast( "string", ARGUMENTS.Line ) ) GTE 0)>
<!--- This was a valid pickup line. --->
<cfreturn (
"Hey, my name is " & THIS.FirstName & "." &
" Why don't you buy me a drink?"
) />
<cfelse>
<!--- This was NOT a valid pickup line. --->
<cfreturn (
"Does that line usually work with a woman? " &
"Maybe you should try something else."
) />
</cfif>
</cffunction>
</cfcomponent>
<!--- Create a set of pickup lines. --->
<cfset arrLines = ListToArray(
"Hey baby, what's your sign?|" &
"Excuse me miss, I think you are a hottie!|" &
"Whoa! Where have you been all my life?!?",
"|"
) />
<!---
Create a Girl object. Notice that we are not using
ColdFusion's CreateObject() method directly. Instead we
are going through the proxy method that calls it for us.
We then, initialize the returned object just as we would
for the standard CreateObject() method call.
--->
<cfset Girl = APPLICATION.CreateCFC( "Girl" ).Init(
FirstName = "Libby",
LastName = "Smith",
PickupLines = arrLines
) />
<!--- Try to pick up the girl. --->
<p>
#Girl.TryPickupLine(
"Can I buy you a drink?"
)#
</p>
<p>
#Girl.TryPickupLine(
"You look hot in that dress!"
)#
</p>
<p>
#Girl.TryPickupLine(
"Whoa! Where have you been all my life?!?"
)#
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment