cfc通过include引入附加功能 [英] cfc that bring in additional functions via include

查看:223
本文介绍了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文件。


使用component.cfc文件 p>

所有CFC自动扩展ColdFusion
WEB-INF / cftags / component.cfc组件。 (WEB-INF目录位于
ColdFusion中的cf_root / wwwroot目录,配置了
嵌入式J2EE服务器,当您在J2EE服务器上部署
ColdFusion时,它位于cf_root目录中。此CFC作为零长度
文件分发。 您可以将其用于任何您希望ColdFusion应用程序服务器实例中的所有CFC继承的核心方法或属性。



注意:当您安装较新版本的ColdFusion时,安装
程序会将现有的component.cfc文件替换为新版本。
因此,在升级之前,你应该保存你有
添加到component.cfc文件的任何代码,然后将代码复制到新的
component.cfc文件中。


如果该解决方案是TOO全局的,你可以扩展你的帮助程序cfc,但它必须在每个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.

Using the component.cfc file

All CFCs automatically extend the ColdFusion WEB-INF/cftags/component.cfc component. (The WEB-INF directory is in the cf_root/wwwroot directory on ColdFusion configured with an embedded J2EE server. It is in the cf_root directory when you deploy ColdFusion on a J2EE server.) This CFC is distributed as a zero-length file. You can use it for any core methods or properties that you want all CFCs in your ColdFusion application server instance to inherit.

Note: When you install a newer version of ColdFusion, the installation procedure replaces the existing component.cfc file with a new version. Therefore, before upgrading, you should save any code that you have added to the component.cfc file, and then copy the code into the new 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引入附加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆