• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Created a Global UDF but don't know how to call it, please assist

Participant ,
Aug 06, 2014 Aug 06, 2014

Copy link to clipboard

Copied

I created a cfcomponent and a bunch functions inside it.  One of the function needs to call a process that is also needed by other part of the application. Since this "process" is needed in a different parts of the application, I decided to make this "process"  a global function that can be called from any part of the application. I found a blog from Ben Nadel that explained exactly what I need. But the problem is I don't quite understand how to call the globa UDF, his explanation is not clear to me. Ben created UDF.cfc:

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

       cffunction name="getMessage" access="public" returntype="string" output="false" hint="I return a test message."

     -- Here is where I wrote the process needed by different parts of the application --

        cfreturn "I am defined in the UDF component"
       cffunction

      cfcomponent

     On the Application.cfc he created a URL scope:  cfcomponent output="false"  hint="I define the application settings and event handlers." 

          Define the application:


         cfset this.name = hash( getCurrentTemplatePath() )
         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" ))

    cfcomponent

The part that I don't understand is, how to call getMessage method? Is it: application.cfc.UDF.getMessage(parameter1,parameter2) ??

TOPICS
Getting started

Views

411

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Aug 06, 2014 Aug 06, 2014

Based on that code, you would refer to it as "URL.UDF.getMessage()". 

Where does the line "cfset structAppend(url,createObject( "component", "UDF" ))" actually occur inside Application.cfc (above any function definitions or inside one of the functions)?  If it's at the top of the component definition, that UDF gets instantiated on each request and put in that page's URL scope.  Does the UDF component maintain state in any way?  If not, and you make sure all of its methods are thread-safe, I woul

...

Votes

Translate

Translate
Guide ,
Aug 06, 2014 Aug 06, 2014

Copy link to clipboard

Copied

LATEST

Based on that code, you would refer to it as "URL.UDF.getMessage()". 

Where does the line "cfset structAppend(url,createObject( "component", "UDF" ))" actually occur inside Application.cfc (above any function definitions or inside one of the functions)?  If it's at the top of the component definition, that UDF gets instantiated on each request and put in that page's URL scope.  Does the UDF component maintain state in any way?  If not, and you make sure all of its methods are thread-safe, I would just store it in the application scope like this:

<cfset application.UDF = createObject("component","UDF")>

Put that in your onApplicationStart() method in Application.cfc so that it is only instantiated once.  Then call it's methods from anywhere in your application as "application.UDF.getMessage()".

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation