-
-
Save tmeers/6ccc5e90bbe4f90adf40 to your computer and use it in GitHub Desktop.
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> |
Because of twitter conversation.
Something tells me my version of ColdFusion isn't going to let me do <cfscript> component { .. } </cfscript> in my Apalication.cfc... Boo
— Tim 'Paul' Meers (@_neckbeard) January 16, 2015
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
I've updated my code with this:
<cfcomponent displayname="Application" output="true" hint="Handle the application.">
<cfscript>
component {
this.directory = getDirectoryFromPath( getCurrentTemplatePath() );
this.mappings = {
"/includes" = this.directory & "includes/";
};
}
</cfscript>
But am receiving the error:
Invalid CFML construct found on line 3 at column 19.
ColdFusion was looking at the following text:
{
The CFML compiler was processing:
A script statement beginning with component on line 3, column 9.
A cfscript tag beginning on line 2, column 10.
Even just wrapping it in an undecorated tag I'm still getting the same compiler error;
<cfcomponent>
<cfscript>
component {
this.directory = getDirectoryFromPath( getCurrentTemplatePath() );
this.mappings = {
"/includes" = this.directory & "includes/";
};
}
</cfscript>
</cfcomponent>
Look at my code sample above. You don't re-declare the component- it's already declared via the cfcomponent tag.
<cfcomponent>
</cfcomponent>
is the equivalent of
component {
}
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>
You can't do 100% script CFCs until ColdFusion 9. In CF8, you can just slap a cfscript tag inside your component declaration and then pretend it's a script CFC.
Remember, this is just syntax-- all functionality is still available to you regardless of whether you use script or tags.