如何获取在 ColdFusion 中扩展我的组件的名称?
假设我有以下名为 Base 的组件:
<cfcomponent output="false">
<cffunction name="init" access="public" returntype="Any" output="false">
<cfset variables.metadata = getmetadata(this)>
<cfreturn this>
</cffunction>
<cffunction name="getmeta" access="public" returntype="Any" output="false">
<cfreturn variables.metadata>
</cffunction>
</cfcomponent>
并且我想在另一个名为 Admin 的组件中扩展 base:
<cfcomponent output="false" extends="Base">
</cfcomponent>
现在,在我的应用程序中,如果我在创建对象时执行以下操作:
<cfset obj = createobject("component", "Admin").init()>
<cfdump var="#obj.getmeta()#">
我收到的元数据告诉我组件的名称是 Admin 并且它正在扩展我的 基础组件。 这一切都很好,但我不想在创建对象时显式调用 init() 方法。
如果我可以在我的 Base 组件中执行类似的操作,我会很高兴:
<cfcomponent output="false">
<cfset init()>
<cffunction name="init" access="public" returntype="Any" output="false">
<cfset variables.metadata = getmetadata(this)>
<cfreturn this>
</cffunction>
<cffunction name="getmeta" access="public" returntype="Any" output="false">
<cfreturn variables.metadata>
</cffunction>
</cfcomponent>
但是 getmeta() 方法返回的元数据告诉我组件名称是 基础,尽管它仍在扩展。 关于如何实现这一目标有什么想法吗?
Let's say I have the following component called Base:
<cfcomponent output="false">
<cffunction name="init" access="public" returntype="Any" output="false">
<cfset variables.metadata = getmetadata(this)>
<cfreturn this>
</cffunction>
<cffunction name="getmeta" access="public" returntype="Any" output="false">
<cfreturn variables.metadata>
</cffunction>
</cfcomponent>
and I want to extend base in another component called Admin:
<cfcomponent output="false" extends="Base">
</cfcomponent>
Now within my application if I do the following when creating the object:
<cfset obj = createobject("component", "Admin").init()>
<cfdump var="#obj.getmeta()#">
The metadata I get back tells me that the name of the component is Admin and it's extending my Base component. That's all good, but I don't want to have to call the init() method explicitly when creating the object.
I would be nice if I could do something like this in my Base component:
<cfcomponent output="false">
<cfset init()>
<cffunction name="init" access="public" returntype="Any" output="false">
<cfset variables.metadata = getmetadata(this)>
<cfreturn this>
</cffunction>
<cffunction name="getmeta" access="public" returntype="Any" output="false">
<cfreturn variables.metadata>
</cffunction>
</cfcomponent>
However then the metadata returned by the getmeta() method telling me that the component name is Base even though it's still being extended. Any thoughts on how to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不是 100% 确定您想要什么,但 ColdFusion 8 添加了 getComponentMetaData() 函数,该函数不需要实例化 CFC,而是采用指向 CFC 的点表示法路径。 您应该能够从 Admin 获取路径,您可以将其传递给 getComponentMetaData(),而无需调用 Base 上的 init() 。
ColdFusion LiveDoc:getComponentMetaData()
I'm not 100% sure what you are after, but ColdFusion 8 added the getComponentMetaData() function that instead of requiring an instantiated CFC, takes a dot notation path to the CFC. You should be able to get the path from Admin which you can pass to getComponentMetaData() without calling the init() on Base.
ColdFusion LiveDoc: getComponentMetaData()
6 岁,但我会给出真正的答案...
给定 Base.cfc:
和 Child.cfc:
要找出 Child 扩展的组件,只需执行以下操作:
6 years old, but I'll give the real answer...
Given Base.cfc:
And Child.cfc:
To find out what component Child extends, just do this:
您是否有理由不想在每个扩展 cfc 中调用 init ?
这似乎按照您想要的方式填充元数据。
Is there a reason you don't want to call init in each extending cfc?
That seems to populate the metadata like you want.