Coldfusion this.mappings 在 cfc 中不起作用 ->功能
如何使我在 application.cfc 中定义的映射能够在其他 cfc 中的其他函数中工作?
即 this.mappings["plugins"] 在任何页面上都可以正常工作,但如果我尝试实例化一个包含调用 this.mappings["plugins"] 的函数的 cfc - 它会失败。
谢谢
编辑: 我不确定 - 这就是我想做的: 在 application.cfc 中:
this.mappings["Plugins"] = \
getDirectoryFromPath(getCurrentTemplatePath())&'Assets/Plugins';
和 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您认为映射是错误的。使用您在 application.cfc 中的定义:
然后将由“插件”在其他代码中引用,因此:
HTH,如果不在调用页面上发布您的代码。
I think you are calling the mapping wrong. Using your definition in application.cfc:
Would then be referenced in other code by "plugins" so:
HTH, if not post your code on the calling page.
在 CFC(Application.cfc 就是其中之一)内,“this”范围仅与该特定 CFC 相关。因此,当您处于 CFM 页面时,该页面属于 Application.cfc 的管辖范围,那么“this”范围适用于 Application.cfc,但当您处于 CFC 中时,它适用于该特定的 CFC。
也就是说,为什么需要直接访问映射结构?如果您想使用该映射来加载对象或包含文件,您只需执行
或< ;cfset obj = createobject("组件","plugins.path.to.my.cfc") />
.您需要直接访问结构的用例是什么?您是否正在尝试修改它?
*编辑修复代码
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
除非 CF9 中发生了变化,否则您在定义映射键的代码中的第一个错误是在每个映射名称的开头没有斜杠“/”。
您正在定义映射,因为
它应该是
注意结构键名称中的斜杠“/”。您必须以这种方式命名每个映射。
然后你可以参考 Sam Farmer 在他的评论中提到的映射”
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
It should instead be
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"