我正在使用this answer中详述的技术来管理小型实用程序函数库。 (基本上,每个函数都使用cfinclude
作为“混合”加载。)
cfdump
仅显示直接在CFC中编写的init
函数。
更多细节:
我正在OnApplicationStart()中的应用程序范围内创建对象。
<cfset application.udfs=createObject("component","extensions.udfs").init()>
但是,为了避免开发人员不得不经常编写application.udfs.foo()
,我认为我会抓住所有函数并将它们放入OnRequestStart()中的变量范围,以便这些假设的开发人员可以写foo()
。
<cfset foo=application.udfs.foo>
显然,这需要是动态的,并且对于对象中的每个函数都要发生,无论有多少。如果我为每个函数重复这一行,那么我已经失去了通过动态生成的库获得的任何东西。
我想也许我可以使用收集循环,但那是无效的。我相当确定有一种方法可以获取对象中的方法列表,但我还没有找到它。
任何线索?
通过by,我的后备将是将application.udfs对象复制到一个具有漂亮短名称的本地对象(如“u”),以便开发人员可以只键入u.foo()
,所以没有需要建议如果我想做的事情无法完成。
答案 0 :(得分:3)
这应该允许您将所有udfs导入全局变量范围:
StructAppend(variables, application.udfs);
答案 1 :(得分:1)
我认为GetMetaData可以帮到你。
答案 2 :(得分:1)
这是Ben Nadel建议的另一个有趣选项:
UDF.cfc
<cfcomponent
output="false"
hint="I define user defined functions.">
<cffunction
name="getMessage"
access="public"
returntype="string"
output="false"
hint="I return a test message.">
<cfreturn "I am defined in the UDF component" />
</cffunction>
</cfcomponent>
的Application.cfc
<!--- Define the application. --->
<cfset this.name = hash( getCurrentTemplatePath() ) />
<cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) />
<!---
Add all of our "global" methods to the URL scope. Since
ColdFusion will automatically seach the URL scope for
non-scoped variables, it will find our non-scoped method
names.
--->
<cfset structAppend(
url,
createObject( "component", "UDF" )
) />