Skip to main content
3 of 3
deleted 4 characters in body; edited tags
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Should I treat direct CFC Calls differently than Page Requests?

Should I leave in <cfif reFindNoCase( "\.cfc$" , targetPage ) eq 0> or not?

Direct CFC calls are flushed when the method returns. Either way, onRequestEnd will stop debug output from being returned for AJAX requests.

Keep If Statement

Additional regular expression search on every AJAX call. Direct CFC calls return quicker. Direct CFC call may not be trimmed.

Remove If Statement (inner code executes for all AJAX requests)

Direct CFC calls' value is stored in a variable. AJAX calls to pages return quicker. All AJAX responses are trimmed.

I'm using ColdFusion 8.

<cfcomponent output=    "false">

<cfset this.name=   "AJAX Debug Output" />

<cffunction name=   "onRequestEnd"
        returnType= "void"
        hint=       "I run on the end of requests (how clever).">

    <cfargument name=   "targetPage"
                hint=   "Path from the web root to the requested page." />
                
    <cfif isAJAXRequest()>
        <cfsetting  showDebugOutput=    "false" 
                    enableCFOutputOnly= "true" />

        <!--- Response from CFC is already flushed to browser. Skip output. --->
        <cfif reFindNoCase( "\.cfc$" , targetPage ) eq 0>

            <!--- If AJAXResponse is not set, then generated content is response. --->
            <cfparam    name=       "request.AJAXResponse"
                        default=    "#getPageContext().getOut().getString()#" />

            <cfcontent  reset= "true" />
            <cfoutput>#trim( request.AJAXResponse )#</cfoutput>
            <cfabort />

        </cfif>

    </cfif>
    
</cffunction>

<cffunction name=   "isAJAXRequest" 
            output= "false"
            access= "private"
            hint=   "I check to see if the request came across with ajax headers">

    <cfif structKeyExists( getHTTPRequestData().headers , "X-Requested-With" )>
        <cfreturn true />

    </cfif>

    <cfreturn false />
    
</cffunction>

</cfcomponent>