Skip to content

Instantly share code, notes, and snippets.

@tmeers
Last active August 29, 2015 14:13
Show Gist options
  • Save tmeers/6ccc5e90bbe4f90adf40 to your computer and use it in GitHub Desktop.
Save tmeers/6ccc5e90bbe4f90adf40 to your computer and use it in GitHub Desktop.
ColdFusion CreateObject
I'm hosting multiple (near identical) applicaitons under the ./applications/ folder.
For this example I'm using [testing] to represent the individual applicaitons.
I need to replace this:
<cfset variables.employeeObj = CreateObject("component","applications.psctest.includes.employee")>
with something less unique to the application. Perhaps basing the string literal:
"applications.[testing].includes.employee"
with an application setting in application.cfc in the root of each applicaiton?
Or creating the instance of these different "variables.employeeObj" objects in the applicaiton.cfc?
Either way I'm trying to cut down on the differeces between applications. The current model makes it
very difficult to update code and move it to the different applications.
For an example, I have a basic cfc (booth.cfc) calling a function I'm using in many other places.
./applications/[testing]/booth/booth.cfc
<cfcomponent output="no">
<!--- create instances of the objects that will be needed. --->
<cfset variables.employeeObj = CreateObject("component","applications.psctest.includes.employee")>
<cffunction name="getEmployeeName" access="remote" returntype="string" returnformat="plain">
<cfargument name="employeeID" type="string" required="yes">
<cfreturn employeeObj.getEmployeeName(arguments.employeeID)>
</cffunction>
</cfcomponent>
./applications/[testing]/includes/employee.cfc
<cfcomponent output="no">
<cffunction name="getEmployeeName" access="public" returntype="string">
<cfargument name="employeeId" type="string" required="yes">
<cfquery name="qGetEmpNameloginEmp" datasource="Intranet">
-- get employee data --
</cfquery>
<cfreturn qGetEmpNameloginEmp.EmployeeFullName>
</cffunction>
</cfcomponent>
@bdw429s
Copy link

bdw429s commented Jan 16, 2015

You have a syntax error. Remove the semi-colon from the end of the middle line.

this.mappings = {
  "/includes" = this.directory & "includes/"
};

ColdFusion 8 has crap struct literal support though. You may find places where you just need to use the older, uglier syntax of declaring structs:

this.mappings = structNew();
this.mappings[ "/includes" ] = this.directory & "includes/";

You also should really just upgrade to something newer than 2007 :)

@tmeers
Copy link
Author

tmeers commented Jan 16, 2015

THAT is what I needed!!

Final working version:

    <cfscript>
        this.directory = getDirectoryFromPath( getCurrentTemplatePath() );
        this.mappings = structNew();
        this.mappings[ "/includes" ] = this.directory & "includes/";
    </cfscript>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment