本文介绍了你如何组织你的小型可重复使用的cffunctions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我改组我的ColdFusion的目录结构和好奇的有经验的开发人员CF如何组织小cffunctions库。

I am reorganizing my ColdFusion directory structures and am curious about how experienced CF developers are organizing libraries of smaller cffunctions.

我不像好奇精心制作的组件(对象)是我对数十家的小工具功能,我们都建立了随着时间的推移。

I am not as curious about elaborate components (objects) as I am about the dozens of little utility functions we all build up over time.


  • 请您使用cffunctions一个大的单个文件和cfinclude呢?

  • 请你用一个大单文件作为CFCOMPONENT并呼吁creatobject / CFINVOKE?

  • 请你把每个实用程序cffunction在自己的CFC和调用CREATEOBJECT / CFINVOKE?

  • 请您使用cfimport的taglib语法?

  • 请您使用CustomTags或cfmodule?

  • 请你有更好的办法吗?

由于我不喜欢冗长的语法,我一直只是cfincluding有一堆它共同cffunctions的lib.cfm。我可以将它们重构为分组氯氟烃我的CreateObject上只是对变量的作用域更好的隔离。

Since I don't like verbose syntax I have been just cfincluding a lib.cfm that has a bunch of common cffunctions in it. I may refactor them to grouped cfcs I can createobject on just to have better isolation on variable scopes.

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

这是一个的 2007年我一直在使用这种方法为6月13日,相当一段时间,它的伟大工程!情况因人而异。

This is a reprint of a blog post I did back on June 13, 2007. I've been using this method for quite sometime and it works great! YMMV.

谁不喜欢的用户定义函数(UDF)?如果你做任何编程,机会是你已经广泛使用它们。人们与他们的最大问题是如何将其纳入,并在你的应用程序整理。

Who doesn't like user-defined functions (UDFs)? If you have done any programming, chances are that you have used them extensively. The biggest problem that people have with them is how to include and organize them in your application.

我已经发现,大多数人做的是创造一个Utils.cfc或UDFs.cfc和剪切和粘贴他们的UDF,他们希望使用到组件,如下所示:

What I've found that most people do is create a Utils.cfc or UDFs.cfc and cut and paste their UDFs that they want to use into the component as demonstrated below:

<!--- UDFs.cfc --->
<cfcomponent output="false">

<cffunction name="init" access="public" returntype="Any" output="false">
  <cfreturn this>
</cffunction>

<cffunction name="myUDF1" access="public" returntype="Any" output="false">
</cffunction>

<cffunction name="myUDF2" access="public" returntype="Any" output="false">
</cffunction>

</cfcomponent>

一旦你把所有您的应用程序将使用粘贴到你的组件的UDF,你需要做的UDF提供给您的应用程序。几乎每个人我见过组件到应用范围,这是否加载。下面的代码行放入 onApplicationStart()如果你使用的Application.cfc或只需添加入Application.cfm如果你正在使用的:

Once you have all the UDFs that your application will be using pasted into your component, you will need to make the UDFs available to your application. Almost everyone I've seen does this loading by the component into the application scope. The following line is placed into the onApplicationStart() if you're using Application.cfc or by just adding it into the Application.cfm if you're using that:

<cfset application.functions = CreateObject("component", "udfs").init()>

哪一个你正在使用,或的Application.cfc Application.cfm,其结果都是一样的;所有的UDF是提供给您的应用程序,你可以在整个自由使用它们。唯一的区别是你用什么变量名。我用application.functions,一些使用application.utils或application.udfs;没关系,再次,结果都相同。

Whichever one you're using, Application.cfc or Application.cfm, the results are the same; all your UDFs are available to your application and you can use them freely throughout. The only difference is what variable name you use. I use application.functions, some use application.utils or application.udfs; doesn’t matter, again, the results are the same.

有一个问题,我有这种方法,虽然,它的繁琐和UDF的组件将获得巨大的。具有如此巨大的组件文件的问题是编辑变得因为通过code线的千滚动的噩梦是不是很有趣,我也已经注意到,CFEclipse停滞不前的大文件。当然code倒闭确实提供了一些缓解,但必须有一个更好的办法。

There is one problem that I have with this approach though, it's cumbersome and the UDFs component will get huge. The problem with having such a huge component file is editing it becomes a nightmare since scrolling through thousand of lines of code isn't very fun and also I've noticed that CFEclipse bogs down on huge files. Sure code collapse does provide some relief but there has to be a better way.

我想要的是只是有一个文件,我使用每个UDF和我的应用程序来自动加载它们的方式。这背后的原因是这样,如果我需要修改 myUDF1 ,我可以打开该文件 myUDF1.cfm 和编辑我需要的东西。我也希望能够从抢到的UDF ,只是拖放到我的应用程序,而无需编辑任何东西。如果我需要从我的应用程序中删除一个UDF,这将是为删除UDF文件,并​​重新初始化我的应用程序一样简单。

What I wanted was to just have one file for each UDF I was using and a way for my application to load them automatically. The reason behind this was so that if I needed to edit myUDF1, I could just open the file myUDF1.cfm and edit what I needed. I also wanted to be able to grab UDFs from CFLib.org and just drop them into my application without having to edit anything. If I ever needed to remove a UDF from my application, it would be as easy as deleting the UDF file and reinitializing my application.

要完成我想要的,我修改了UDFs.cfc至code的11行:

To accomplish what I wanted, I modified my UDFs.cfc to 11 lines of code:

<!--- UDFs.cfc --->
<cfcomponent output="false">

  <cfset variables.udfdir = GetDirectoryFromPath(GetCurrentTemplatePath()) & "udfs">
  <cfset variables.q = "">

  <cffunction name="init" access="public" returntype="Any" output="false">
    <cfreturn this>
  </cffunction>

  <cfdirectory action="list" directory="#variables.udfdir#" filter="*.cfm" name="variables.q">

  <cfoutput query="variables.q">
    <cfinclude template="udfs\#name#">
  </cfoutput>

</cfcomponent>

那么,究竟是怎么回事?

So what exactly is going on?

在简单地说,这里发生的事情:我有一个名为的UDF 在我有我的UDFs.cfc相同的目录目录。这就是我把所有我的UDF CFM文件的目录。什么UDFs.cfc确实是这个目录进行扫描,当它被调用,并自动将其包括找到的每个CFM文件。因此,它会自动加载中的UDF文件夹中任何的UDF到自身(通常称为混入)。

In a nutshell, here's what’s happening: I have a directory called udfs in the same directory that I have my UDFs.cfc. This is the directory that I put all of my UDF CFM files. What the UDFs.cfc does is scan this directory when it is called and automatically includes each CFM file it finds. Thus it automatically loads any UDFs in the UDFs folder into itself (commonly called a "mixin").

所以,在达到我的目标!我在自己的文件中的每个UDF所以我没有通过一个巨大的组件文件滚动找到它。我现在可以打开,方便地编辑。通过看目录,我知道我的UDF应用程序使用。我可以自动从CFLib.org只需保存从浏览器中的文本目录中的文件添加UDF。另外,如果我不再需要使用UDF在我的应用程序,我只是删除从目录中的文件,并将其从下一重新初始化过程中我的应用程序中删除。所有这一切都无需触摸主UDFs.cfc文件中完成的。

So my goal is reached! I have each UDF in its own file so I don't have to scroll through a huge component file to find it. I can now open and edit it easily. By just looking at the directory, I know what UDFs my application is using. I can automatically add a UDF from CFLib.org by just saving the text from browser into a file in the directory. Plus if I no longer need to use the UDF in my application, I simply delete the file from the directory and it's removed from my application during the next re-init. All this is done without having to touch the main UDFs.cfc file.

下面是什么样的UDF CFM文件之一的样子的例子。该文件称为 fullLeft.cfm 并驻留在UDF的目录。

Below is an example of what one of the UDF CFM files looks like. The file is called fullLeft.cfm and resides in the UDFs directory.

<!--- fullLeft --->
<cffunction name="fullLeft" access="public" displayname="fullLeft" returntype="string" output="false">
  <cfargument name="str" type="string" required="true">
  <cfargument name="count" type="numeric" required="true">
  <cfif not refind("[[:space:]]", arguments.str) or (arguments.count gte len(arguments.str))>
    <cfreturn Left(arguments.str, arguments.count)>
  <cfelseif reFind("[[:space:]]",mid(arguments.str,arguments.count+1,1))>
    <cfreturn left(arguments.str,arguments.count)>
  <cfelse>
    <cfif count-refind("[[:space:]]", reverse(mid(arguments.str,1,arguments.count)))>
      <cfreturn Left(arguments.str, (arguments.count-refind("[[:space:]]", reverse(mid(str,1,arguments.count)))))>
    <cfelse>
      <cfreturn left(arguments.str,1)>
    </cfif>
  </cfif>
</cffunction>

这篇关于你如何组织你的小型可重复使用的cffunctions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 16:11