Highlighted

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

Explorer ,
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) ??

Most Valuable Participant
Correct answer by Carl_Von_Stetten | Most Valuable Participant

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.

TOPICS
Getting started

Views

337

Likes

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

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

Explorer ,
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) ??

Most Valuable Participant
Correct answer by Carl_Von_Stetten | Most Valuable Participant

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.

TOPICS
Getting started

Views

338

Likes

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
Aug 06, 2014 0
Most Valuable Participant ,
Aug 06, 2014

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Aug 06, 2014 0