Coldfusion this.mappings 在 cfc 中不起作用 ->功能

发布于 2024-10-12 02:07:37 字数 704 浏览 2 评论 0原文

如何使我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱没勇气 2024-10-19 02:07:37

我认为您认为映射是错误的。使用您在 application.cfc 中的定义:

this.mappings["plugins"]

然后将由“插件”在其他代码中引用,因此:

var aName = new plugins.theCFC()
var aName = createObject("component","plugins.theCFC").init()
<cfinclude template="/plugins/aFile.cfm">

HTH,如果不在调用页面上发布您的代码。

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.

魂牵梦绕锁你心扉 2024-10-19 02:07:37

在 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

伪心 2024-10-19 02:07:37

除非 CF9 中发生了变化,否则您在定义映射键的代码中的第一个错误是在每个映射名称的开头没有斜杠“/”。

您正在定义映射,因为

this.mappings["plugins"] =

它应该是

this.mappings["/plugins"] =

注意结构键名称中的斜杠“/”。您必须以这种方式命名每个映射。

然后你可以参考 Sam Farmer 在他的评论中提到的映射”

然后将由“插件”在其他代码中引用,因此:

var aName = new plugins.theCFC()
var aName = createObject("component","plugins.theCFC").init()

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">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文