2

我目前正在尝试扩展第三方 CFC,一切正常并且工作正常,但是,我现在已经覆盖了 CFC 中的一个方法(我首先扩展它的全部原因)。现在第三方 CFC 都是基于标签的,并且有一个名为“do”的函数 - 定义如下:

<cffunction name="do" returntype="any" access="public" output="true" 
                hint="I compile and execute a specific fuseaction.">
        <cfargument name="action" type="string" required="true" 
                    hint="I am the full name of the requested fuseaction (circuit.fuseaction)." />
        <cfargument name="contentVariable" type="string" default="" 
                    hint="I indicate an attributes / event scope variable in which to store the output." />
        <cfargument name="returnOutput" type="boolean" default="false" 
                    hint="I indicate whether to display output (false - default) or return the output (true)." />
        <cfargument name="append" type="boolean" default="false" 
                    hint="I indicate whether to append output (false - default) to the content variable." />
        <cfargument name="passThroughReturn" type="boolean" default="false" 
                    hint="I indicate whether to allow for a return to be passed through from an action CFC." />

现在,我的 CFC 都是 cfscript(我的个人偏好和项目编码标准)。如果我尝试在我的 CFC 中覆盖此方法,如下所示:

public any function do( Required String action, String contentVariable="", boolean returnOutput=false, boolean append=false, boolean passThroughReturn=false){

然后我得到一个关于函数名的错误,假设是因为“do”是一个 CF 保留字。

我试图通过重命名方法并仅映射来解决这个问题,例如:

this.do = variables.invokeDo;

public any function invokeDo( Required String action, String contentVariable="", boolean returnOutput=false, boolean append=false, boolean passThroughReturn=false){

这绕过了错误,但如果我调用myObject.do(..)它只是调用超类方法。

有谁知道我如何在 CFscript 中覆盖这个方法?

4

2 回答 2

1

不幸的是,我不认为有一个很好的方法来完成你想要的。但是,有一些解决方法。一种技巧是这样做:

component extends="OriginalComponent" {
  variables["do"] = function () {
    // new function code here
  };

  this["do"] = variables["do"];
}

实际上并没有覆盖传统意义上的函数,但它似乎起作用:在测试时,对函数的内部和外部调用都称为新函数,而不是原始函数。

我不知道这种黑客攻击可能会产生其他后果,所以要小心。

于 2013-07-04T05:13:47.353 回答
0

如果您真的不能使用 CFML,如果您的新 cfc 不需要进行类型检查,并且您不需要访问父级的私有变量,这可能会起作用。

component {

  function init(parent) {
    variables.parent = parent;
    return this;
  }

  function onMissingMethod(missingMethodName, missingMethodArguments)
  {
    if (missingMethodName == "do")
      doFunc();
    else {
      // if CF10, use invoke(), else use yucky evaluate()
    }
  }

  function doFunc()
  {
     // your new do() function
  }
}
于 2013-07-04T00:58:29.103 回答