1

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 ---

Evik James
  • 10,335
  • 18
  • 71
  • 122
  • Do you get an error when you try to set the udf variable in Application.cfm? If so, what is the error? – Jake Feasel Feb 01 '12 at 17:23
  • Are you sure that the application.cfm is firing this line of code? Anything you put in application.cfm will run before the page is processed, but usually you add initialization controls around quite a bit of this code. – J.T. Feb 01 '12 at 17:41
  • @JakeFeasel I do not get an error when I add this in the application.cfm: . – Evik James Feb 01 '12 at 18:05
  • @JasonTabler I put this line of code as the very, very last statement in the application.cfm. It is completely outside of anything else in the application.cfm file. It doesn't work. – Evik James Feb 01 '12 at 18:06
  • Okay, if you don't get an error, then what is the value of UDF after you set it in Application.cfm? What does `` show? – Jake Feasel Feb 01 '12 at 18:08
  • Great question! Nothing. The dump does absolutely NOTHING from within the application.cfm file, which is bizzaro! When I copy and past that same dump (exactly as you have written) into other pages, I get the "UDF is not defined error". When I change the dump to "APPLICATION.AppOBJ.udf_library" I see my functions. – Evik James Feb 01 '12 at 18:11
  • 1
    [Ben Nadel](http://www.bennadel.com) come up with an interesting method which allows you to access the UDFs as if they were built in functions: http://www.bennadel.com/blog/1776-Creating-Globally-Accessible-User-Defined-Functions-In-ColdFusion-Safer-Version-.htm – Micah Feb 01 '12 at 18:53
  • 1
    See also: http://stackoverflow.com/questions/624541/how-do-you-organize-your-small-reusable-cffunctions – ale Feb 01 '12 at 20:51

1 Answers1

2

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 ) >
Micah
  • 1,221
  • 17
  • 42
  • Micah, I did read Ben's blog on this. I read it long ago too. It didn't apply to me then though. This answered my question perfectly. This was EXACTLY what I needed. Thanks for helping out. I'll put my finished code above. – Evik James Feb 01 '12 at 20:46
  • It takes two to tango. You reintroduced me to that specific information exactly when I needed it. That was the big help! You made the information relevant. That's pretty awesome! Besides, I give props on a daily basis already. – Evik James Feb 01 '12 at 22:28