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

时间:2012-02-01 17:02:14

标签: coldfusion coldfusion-9 coldfusion-8 cfc

我正在使用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 ---

1 个答案:

答案 0 :(得分:2)

我还没有尝试过,但我认为这应该有效,因为这个想法来自Ben Nadel's blog条目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>

您现在应该可以全局访问MyFunction()

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

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