本文介绍了cfc通过include引入附加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的application.cfc开头为:



< cfcomponent extends =org.corfield.framework>



稍后在 void function setupApplication()output =false{



我有

  //工具
包括initapp.cfm

initapp.cfm只有函数。例如:

 <!--- Helper functions ---& 
< cfscript>
string function stripHTML(str)output =false{
return REReplaceNoCase(arguments.str,< [^>] *,,ALL);
}

application.stripHTML = stripHTML;

< / cfscript>

函数的性质与会话无关。

解决方案

如果你试图把帮助函数放在一起用于CFCs ,一个选项可能是使用component.cfc文件。

如果该解决方案是TOO全局的,你可以扩展你的帮助程序cfc,但它必须在每个cfc中完成,你的一次性设置和忘记它的想法。请参见



如果你的帮助函数用于.cfm文件,我会像亚当建议的。我通常把我的帮助函数放在CFC文件夹中的toolscfc中,并将其作为一个应用程序作用域cfc。

  onApplicationStart(){
application.tools = createObject(component,cfc.tools);
}

我的一个帮助函数记录索引索引集合所需的时间。使用它看起来像

 < cfset application.tools.logSolrIndex('collectionName',getTickCount() -  start,qFileList.recordCount )> 

最后手段:
如果你不得不坚持包括在application.cfc之外使用,我可能只需要包括initapp.cfm onRequest(),然后才能包含你的页面。


My application.cfc starts with:

<cfcomponent extends="org.corfield.framework">

later on void function setupApplication() output="false" {

I have

// tools
include "initapp.cfm";

initapp.cfm has nothing but functions in it. Things like:

<!--- Helper functions --->
<cfscript>
string function stripHTML(str) output="false" {
return REReplaceNoCase(arguments.str,"<[^>]*>","","ALL");
}

application.stripHTML = stripHTML;

</cfscript>

The nature of the functions is NOT associated with a session. Is there a better way to make functions available globally?

解决方案

If you're trying to put helper functions together for use in CFCs, one option may be to use the component.cfc file.

If that solution is TOO global you can extend your helper cfc, but it has to be done in every cfc and doesn't answer your one-time-set-it-and-forget-it idea. See Using CFCs effectively

If your helper functions are for use in .cfm files, I'd do like Adam suggested. I usually put my helper functions in a "tools" cfc located in a CFC folder and make it an application scoped cfc.

function onApplicationStart(){
    application.tools = createObject("component", "cfc.tools");
}

One of my helper functions logs the time it takes to index a solr collection. Using it looks like

<cfset application.tools.logSolrIndex('collectionName',getTickCount()-start,qFileList.recordCount)>

Last resort:If you had to stick with an include for use outside of the application.cfc, I might simply include initapp.cfm onRequest() before you include your page.

这篇关于cfc通过include引入附加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 14:54