如何获取在ColdFusion中扩展我的组件的名称?

时间:2008-12-12 18:49:56

标签: coldfusion components cfc

假设我有以下名为 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 的组件中扩展基础:

<cfcomponent output="false" extends="Base">
</cfcomponent>

现在在我的应用程序中,如果我在创建对象时执行以下操作:

<cfset obj = createobject("component", "Admin").init()>
<cfdump var="#obj.getmeta()#">

我收到的元数据告诉我,该组件的名称是 管理 ,它正在扩展我的 Base 组件。这一切都很好,但我不想在创建对象时显式调用 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()方法返回的元数据告诉我组件名称 Base ,即使它仍在扩展。有关如何实现这一点的任何想法?

3 个答案:

答案 0 :(得分:1)

您是否有理由不想在每个扩展cfc中调用init?

<cfcomponent output="false" extends="Base">
    <cfset super.init()>

</cfcomponent>

这似乎填充了你想要的元数据。

答案 1 :(得分:1)

我不是100%确定你的目标,但是ColdFusion 8添加了getComponentMetaData()函数,而不需要实例化的CFC,而是采用点符号路径到CFC。您应该能够从Admin获取路径,您可以传递给getComponentMetaData()而无需在Base上调用init()。

ColdFusion LiveDoc: getComponentMetaData()

答案 2 :(得分:1)

6岁,但我会给出真正的答案......

鉴于Base.cfc:

component{
    public function foo(){
        return 'base';
    }
}

和Child.cfc:

component extends="Base"{
    public function foo(){
        return 'child';
    }
}

要找出Child扩展的组件,只需执行以下操作:

<cfscript>
child = createObject( "component", "Child" );
writeDump( getMetaData(child).extends.name );
</cfscript>