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

发布于 2024-07-09 21:14:37 字数 1599 浏览 9 评论 0原文

假设我有以下名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-07-16 21:14:38

我不是 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()

随波逐流 2024-07-16 21:14:38

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>

6 years old, but I'll give the real answer...

Given Base.cfc:

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

And Child.cfc:

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

To find out what component Child extends, just do this:

<cfscript>
child = createObject( "component", "Child" );
writeDump( getMetaData(child).extends.name );
</cfscript>
骷髅 2024-07-16 21:14:37

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

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

</cfcomponent>

这似乎按照您想要的方式填充元数据。

Is there a reason you don't want to call init in each extending cfc?

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

</cfcomponent>

That seems to populate the metadata like you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文