Holy cow! I just realized that Application.cfc-scoped methods such as OnApplicationStart() and OnRequestStart() as well as any other custom user-defined ColdFusion functions (in the Application.cfc) are stored in and are accessible via the current page's VARIABLES scope. Meaning, you can call things like:
Launch code in new window » Download code as text file »
This seems a bit silly to me, but I am glad I'm finally aware of this.
Download Code Snippet ZIP File
Comments (8) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
That's great to know! I have a couple of extra functions in my App.cfc that I use for logout/login and other stuff. I was bummed I couldn't access that same code from other templates. Sweet find!
Posted by Brian on Nov 6, 2006 at 3:57 PM
Yes that is the case with application.cfc. You can set up your own UDF's in the application.cfc and you can call them from any of your pages of course as long as the function is set to public. I've seen some people include it in the onRequestStart() (I think that was the one) but then they have to put it in the request scope just to access it. If you ask me its just much easier to drop in to application.cfc.
Posted by Javier Julio on Nov 6, 2006 at 10:49 PM
I believe this is true only if you have the onRequest method in your app.cfc. I try not to use onRequest anymore because it interferes with web service CFCs and AJAX calls. But if this isn't an issue, it definitely is a cool by-product. I actually learned this from Ray Camden - see the comments for this post: http://ray.camdenfamily.com/index.cfm/2006/9/20/Using-JavaScript-to-warn-a-user-about-a-session-timeout.
Keep up your exploration!
Posted by Rob Pilic on Nov 6, 2006 at 11:15 PM
Rob,
Yeah, I hear you on the AJAX and CFC issues and OnRequest(). Ray Camden came up with a good solution for that by basically deleting the OnRequest() method whenever a CFC is called directly (or some sort of flash remoting, but I don't remember that one).
Posted by Ben Nadel on Nov 7, 2006 at 8:25 AM
What's the best way to make these UDF's defined in the app.cfc available to the code run in custom tags?
Posted by Lems on Jul 24, 2008 at 10:08 AM
@Lems,
I am not sure why you want to do that, but I suppose you could store a reference to the Application.cfc in the REQUEST scope. In your OnRequestStart() method, you could have something like:
<cfset REQUEST.Application = THIS />
Then, in your custom tags you could something like:
<cfset REQUEST.Application.OnRequestEnd() />
Not sure why, but I suppose that would work.
Posted by Ben Nadel on Jul 24, 2008 at 11:48 AM
Maybe I didn't explain it well, but regardless, I found this article to answer my question:
Thanks for the quick reply!
Posted by Lems on Jul 24, 2008 at 12:18 PM
@Lems,
Ok cool. Good luck.
Posted by Ben Nadel on Jul 24, 2008 at 12:24 PM