Last active
August 29, 2015 14:13
-
-
Save tmeers/6ccc5e90bbe4f90adf40 to your computer and use it in GitHub Desktop.
ColdFusion CreateObject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./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> |
Ahhh! I read right over that. But still an error. It's not liking the opening curly brace in the mappings now:
<cfcomponent>
<cfscript>
this.directory = getDirectoryFromPath( getCurrentTemplatePath() );
this.mappings = {
"/includes" = this.directory & "includes/";
};
</cfscript>
</cfcomponent>
Error
Invalid CFML construct found on line 4 at column 41.
ColdFusion was looking at the following text:
{
The CFML compiler was processing:
A script statement beginning with this.mappings on line 4, column 25.
A cfscript tag beginning on line 2, column 10.
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 :)
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
Look at my code sample above. You don't re-declare the component- it's already declared via the cfcomponent tag.
is the equivalent of