ColdFusion 中常用函数的存储位置
我们正在运行 ColdFusion MX7。
我们遇到的一个问题是我们在很多页面中使用了很多函数。 如果让它们存在于“全局”ColdFusion 范围内,而不是必须将它们包含在我们的所有页面中,那就太好了。
有没有一种方法可以做到这一点而不涉及自定义标签等?
我知道我们可以将一些对象附加到应用程序或服务器范围,但我们必须这样引用它们。
只需将它们添加到全局范围中就完美了。
编辑
感谢您的建议,这就是我的想法。 基本上,对于 OnRequestStart 函数中的每个请求,将函数引用 (this.functionName) 分配给客户端范围内正确命名的变量。
Application.cfc:
<cfcomponent OUTPUT="FALSE">
<cfset This.name = "MyApp">
<CFSET This.clientManagement = true>
<CFSET This.SessionManagement = true>
<CFFUNCTION NAME="Coalesce" OUTPUT="FALSE" access="public">
<CFARGUMENT NAME="ARG1">
<CFARGUMENT NAME="ARG2">
<CFIF ARG1 NEQ "">
<CFRETURN ARG1>
<CFELSE>
<CFRETURN ARG2>
</CFIF>
</CFFUNCTION>
<cffunction name="onRequestStart">
<CFSET CLIENT.COALESCE = this.COALESCE>
</cffunction>
</cfcomponent>
该应用程序下的页面高兴地响应号召:
<CFOUTPUT>#COALESCE("ONE","TWO")#</CFOUTPUT>
工作得很好!
We are running ColdFusion MX7.
One problem we have is that we have a lot of functions that we use in a lot of our pages. It would be nice to have them live in the 'Global' ColdFusion scope rather than having to include them in all of our pages.
Is there a way to do this that does not involve custom tags or the like?
I know we could attach some objects to the Application or Server scopes, but then we have to reference them as such.
Simply adding them to the global scope would be perfect.
EDIT
Thanks to the suggestions, here is what I came Up with. Basically, for each request in the OnRequestStart function, assign to a properly named variable in the client scope the function reference (this.functionName).
Application.cfc:
<cfcomponent OUTPUT="FALSE">
<cfset This.name = "MyApp">
<CFSET This.clientManagement = true>
<CFSET This.SessionManagement = true>
<CFFUNCTION NAME="Coalesce" OUTPUT="FALSE" access="public">
<CFARGUMENT NAME="ARG1">
<CFARGUMENT NAME="ARG2">
<CFIF ARG1 NEQ "">
<CFRETURN ARG1>
<CFELSE>
<CFRETURN ARG2>
</CFIF>
</CFFUNCTION>
<cffunction name="onRequestStart">
<CFSET CLIENT.COALESCE = this.COALESCE>
</cffunction>
</cfcomponent>
The pages that are under this application happily respond to the call:
<CFOUTPUT>#COALESCE("ONE","TWO")#</CFOUTPUT>
Works great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不存在“全球范围”这样的东西。
如果您正在讨论每个页面中的变量范围,您可以尝试将 UDF 包含在 Application.cfm 中。
如果您使用 Application.cfc,请在 CF7 文档中查找 onRequest()。
There's no such thing as "global scope".
If you're talking about variables scope in every page, you can try including the UDF's inside Application.cfm.
If you use Application.cfc, look up onRequest() in the CF7 doc.
我很满意的一个选择是在 Application.cfc 中创建一个服务(或类似的命名)组件。 将所有功能添加到此组件并在创建应用程序时创建它。 这将缩短加载时间,因为函数缓存在应用程序中,并且还使该应用程序中的任何文件都可以访问这些函数。 当然,那么你需要调用像 application.services.myUsefulFunction() 这样的函数
One option I have been happy with is to create a services (or similiar named) component in Application.cfc. Add all of your functions to this component and create it when the application is created. This will improve load time as the functions are cached in the application and also make the functions accessible to any file in that application. Of course then you would be required to call the function like application.services.myUsefulFunction()