如何使用缩写名称访问 APPLICATION 范围内的 UDF 库?

发布于 2025-01-01 10:16:17 字数 1080 浏览 2 评论 0原文

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

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

发布评论

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

评论(1

灯角 2025-01-08 10:16:17

我还没有尝试过,但我认为它应该可行,因为这个想法来自 Ben Nadel 的博客 标题为 在 ColdFusion(安全版本)中创建全局可访问的用户定义函数

<cfcomponent output="false" hint="I define the application settings and event handlers.">

    <!--- Define the application. --->
    <cfset this.name = "TestApp" >
    <cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) >


    <!---
        Add all of our "global" methods to the URL scope. Since
        ColdFusion will automatically seach the URL scope for
        non-scoped variables, it will find our non-scoped method
        names.
    --->
    <cfset structAppend( url, createObject( "component", "udf.udf_library" ) ) >

</cfcomponent>

您现在应该能够全局访问 MyFunction()

如果您想以 UDF.MyFunction() 的形式访问该函数,那么我认为您应该能够将 Ben 的示例修改为以下内容:

<cfset UDF = StructNew() >
<cfset structAppend( UDF, createObject( "component", "udf.udf_library" ) ) >
<cfset structAppend( url, UDF ) >

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)

<cfcomponent output="false" hint="I define the application settings and event handlers.">

    <!--- Define the application. --->
    <cfset this.name = "TestApp" >
    <cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) >


    <!---
        Add all of our "global" methods to the URL scope. Since
        ColdFusion will automatically seach the URL scope for
        non-scoped variables, it will find our non-scoped method
        names.
    --->
    <cfset structAppend( url, createObject( "component", "udf.udf_library" ) ) >

</cfcomponent>

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:

<cfset UDF = StructNew() >
<cfset structAppend( UDF, createObject( "component", "udf.udf_library" ) ) >
<cfset structAppend( url, UDF ) >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文