coldfusion this.mappings在CFC->函数中不起作用
-
11-10-2019 - |
题
如何获得我在application.cfc中定义的映射以在其他CFC中的其他功能中工作?
即this.mappings [“ plugins”]在任何页面上都可以正常工作,但是如果我尝试实例化包含调用此功能的函数的CFC,则mappings ['plugins'] - 它会失败。
谢谢
编辑:我不确定 - 这是我要做的:在application.cfc中:
this.mappings["Plugins"] = \
getDirectoryFromPath(getCurrentTemplatePath())&'Assets/Plugins';
和库存。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>
解决方案
我认为您称映射错误。在application.cfc中使用您的定义:
this.mappings["plugins"]
然后将通过“插件”在其他代码中引用:
var aName = new plugins.theCFC()
var aName = createObject("component","plugins.theCFC").init()
<cfinclude template="/plugins/aFile.cfm">
hth,如果不在呼叫页面上发布您的代码。
其他提示
在CFC中,其中一个应用程序。CFC是一个,“此”范围仅与该特定CFC有关。因此,当您在CFM页面中时,属于应用程序。CFC的管辖权,则“此”范围是用于application.cfc的范围,但是当在CFC中,其为特定的CFC。
也就是说,为什么需要直接访问映射结构?如果要使用该映射加载对象或包含文件,则可以做 <cfinclude template="/plugins/path/to/myfile" />
或者 <cfset obj = createobject("component","plugins.path.to.my.cfc") />
.
您需要什么用直接访问结构的用例?您是否要修改它?
*编辑以修复代码
除非CF9中的情况发生了变化,否则您在每个映射名称开头的无斜杠“/”的代码中的第一个错误。
您将映射定义为
this.mappings["plugins"] =
相反应该是
this.mappings["/plugins"] =
注意结构密钥名称中的斜杠“/”。您必须以这种方式命名每个映射。
然后,您将指定的映射像Sam Farmer在他的评论中提到的那样。
然后将通过“插件”在其他代码中引用:
var aName = new plugins.theCFC() var aName = createObject("component","plugins.theCFC").init() <cfinclude template="/plugins/aFile.cfm">
不隶属于 StackOverflow