Coldfusion this.mappings does not work in a cfc -> function
-
11-10-2019 - |
Question
How do I get the mappings I have defined in application.cfc to work in other functions in other cfcs?
i.e. this.mappings["plugins"] works just fine on any page but if I try to instantiate a cfc containing a function that calls this.mappings["plugins"] - it fails.
thanks
EDIT: I'm not sure - here's what I am trying to do: In application.cfc:
this.mappings["Plugins"] = \
getDirectoryFromPath(getCurrentTemplatePath())&'Assets/Plugins';
and in stock.cfc:
<cfcomponent output="yes" >
<cffunction name="showIndecies" access="public" output="yes" returntype="string">
<cfscript>
j = 1;
variables.indeciesArray = ArrayNew(1);
variables.indeciesFile = \
application.mappings["Plugins"]&'/StockMarketData/Data/indecies.csv';
</cfscript>
Solution
I think you are calling the mapping wrong. Using your definition in application.cfc:
this.mappings["plugins"]
Would then be referenced in other code by "plugins" so:
var aName = new plugins.theCFC()
var aName = createObject("component","plugins.theCFC").init()
<cfinclude template="/plugins/aFile.cfm">
HTH, if not post your code on the calling page.
OTHER TIPS
Inside a CFC, of which Application.cfc is one, the "this" scope only pertains to that specific CFC. So when you are in a CFM page, that falls under the Application.cfc's jurisdiction, then the "this" scope is for the Application.cfc, but when in a CFC, its for that particular CFC.
That said, why would you need to access the mappings struct directly? If you want to use that mapping to load an object or include a file, you can just do <cfinclude template="/plugins/path/to/myfile" />
or <cfset obj = createobject("component","plugins.path.to.my.cfc") />
.
What is your use case for needing to access the struct directly? Are you trying to modify it?
*edited to fix code
Unless things have changed in CF9, your first mistake in the code that is defining the mapping keys without a slash "/" at the beginning of each mapping name.
You are defining the mappings as
this.mappings["plugins"] =
It should instead be
this.mappings["/plugins"] =
Notice the slash "/" in the structure key name. You must name each mapping that way.
Then you'd refer to the mappings as Sam Farmer mentioned in his comment"
Would then be referenced in other code by "plugins" so:
var aName = new plugins.theCFC() var aName = createObject("component","plugins.theCFC").init() <cfinclude template="/plugins/aFile.cfm">