tags:

views:

288

answers:

3

We are running ColdFusion MX7.

One problem we have is that we have a lot of functions that we use in a lot of our pages. It would be nice to have them live in the 'Global' ColdFusion scope rather than having to include them in all of our pages.

Is there a way to do this that does not involve custom tags or the like?

I know we could attach some objects to the Application or Server scopes, but then we have to reference them as such.

Simply adding them to the global scope would be perfect.

EDIT

Thanks to the suggestions, here is what I came Up with. Basically, for each request in the OnRequestStart function, assign to a properly named variable in the client scope the function reference (this.functionName).

Application.cfc:

<cfcomponent OUTPUT="FALSE">
<cfset This.name = "MyApp">
<CFSET This.clientManagement = true>
<CFSET This.SessionManagement = true>

<CFFUNCTION NAME="Coalesce" OUTPUT="FALSE" access="public">
 <CFARGUMENT NAME="ARG1">
 <CFARGUMENT NAME="ARG2">

 <CFIF ARG1 NEQ "">
  <CFRETURN ARG1>
 <CFELSE>
  <CFRETURN ARG2>
 </CFIF>
</CFFUNCTION>

<cffunction name="onRequestStart">
 <CFSET CLIENT.COALESCE = this.COALESCE>
</cffunction>

</cfcomponent>

The pages that are under this application happily respond to the call:

<CFOUTPUT>#COALESCE("ONE","TWO")#</CFOUTPUT>

Works great!

+6  A: 

There's no such thing as "global scope".

If you're talking about variables scope in every page, you can try including the UDF's inside Application.cfm.

If you use Application.cfc, look up onRequest() in the CF7 doc.

Henry
By 'Global Scope' I mean the scope that CF puts standard functions like now(). I'm not sure where it stores them.
Tom Hubbard
Well, that "magic global scope" is not accessible by us. :)
Henry
Well it darn well should be. ;o)
Tom Hubbard
Well if it did, then someone can override your system function like now(), would you be happy about that? :D
Henry
Peter Boughton
+2  A: 

One option I have been happy with is to create a services (or similiar named) component in Application.cfc. Add all of your functions to this component and create it when the application is created. This will improve load time as the functions are cached in the application and also make the functions accessible to any file in that application. Of course then you would be required to call the function like application.services.myUsefulFunction()

Nick
+2  A: 

Check out this similar post. It has worked great for me...

How do you organize your small reusable functions

kevink