如何使用缩写名称访问 APPLICATION 范围内的 UDF 库?
我正在使用 ColdFusion 8.0.1。
我创建了一个 UDF 库并将其放入 CFC 中。我将库加载到 APPLICTION 范围中,如下所示:
// CREATE STRUCTURE OBJECTS
if (not isDefined("APPLICATION.AppOBJ") or not isStruct(APPLICATION.AppOBJ)) {
APPLICATION.AppOBJ = structNew();
APPLICATION.AppOBJ.udf_library = createObject("component", "udf.udf_library");
}
该库运行良好!但我想减少访问函数所需的代码,以缩短参考。目前,我必须访问这样的函数:
APPLICATION.AppOBJ.udf_library.myFunction();
我希望能够将该库对象引用为“UDF”,如下所示:
UDF.myFunction();
在另一个 ColdFusion 9 项目中(同样,这是一个 CF8 项目!),我能够执行以下操作:在我创建 ojbect 之后
<cfset udf = APPLICATION.AppOBJ.udf_library>
,在当前项目中,这在 application.cfm 文件中不起作用。然而,当我将它放在正在使用的页面上时,它确实可以工作。
我的问题是我可以将最后一行代码放在多远的上游,以使变量在应用程序的任何页面上可用? CF8和CF9对于这种类型的东西有区别吗?差异是因为我在 application.CFM 与 application.CFC 中工作吗?
谢谢!!!
-- 编辑 -- 更多信息 ---
我尝试访问 APPLICATION.AppOBJ.udf_library 对象的文件位于自定义标记内。这可能重要吗?
-- 答案 -- 感谢 MICAH 和 BEN NADEL ---
I am using ColdFusion 8.0.1.
I created a UDF library and put it in a CFC. I load the library in the APPLICTION scope like this:
// CREATE STRUCTURE OBJECTS
if (not isDefined("APPLICATION.AppOBJ") or not isStruct(APPLICATION.AppOBJ)) {
APPLICATION.AppOBJ = structNew();
APPLICATION.AppOBJ.udf_library = createObject("component", "udf.udf_library");
}
The library works great! But I want to reduce the code needed to access the functions, to shorten the reference. Currently, I have to access the functions like this:
APPLICATION.AppOBJ.udf_library.myFunction();
I want to be able to reference this library object as "UDF", like this:
UDF.myFunction();
In another ColdFusion 9 project (Again, this is a CF8 project!), I am able to do this right after I create the ojbect
<cfset udf = APPLICATION.AppOBJ.udf_library>
In the current project, this doesn't work in the application.cfm file. It DOES however, work when I put it on the page that it is being used.
My question is how far upstream can I put this last line of code to have the variable available on any page in the application? Is there a difference between CF8 and CF9 for this type of thing? Is the difference because I am working in application.CFM versus application.CFC?
Thanks!!!
-- EDIT -- MORE INFORMATION ---
The files that I am trying to access the APPLICATION.AppOBJ.udf_library object are within a custom tag. Might that matter?
-- ANSWER -- THANKS TO MICAH AND BEN NADEL ---
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有尝试过,但我认为它应该可行,因为这个想法来自 Ben Nadel 的博客 标题为 在 ColdFusion(安全版本)中创建全局可访问的用户定义函数
您现在应该能够全局访问
MyFunction()
。如果您想以
UDF.MyFunction()
的形式访问该函数,那么我认为您应该能够将 Ben 的示例修改为以下内容:I haven't tried this yet but I think it should work as the idea comes from Ben Nadel's blog entry entitled Creating Globally Accessible User Defined Functions In ColdFusion (Safer Version)
You should now be able to access
MyFunction()
globally.If you want to access the function as
UDF.MyFunction()
then I think you should be able modify Ben's example to the following: