【问题标题】:Coldfusion function within conditional logic条件逻辑中的冷融合功能
【发布时间】:2015-08-07 15:00:28
【问题描述】:

我的 cfc 中的函数有问题。当我尝试引入条件逻辑以将查询分配给网格时,它们的行为很有趣。基本上在 URL 中,我将拥有 ?GRIDID=x,它会告诉 cfc 要运行哪个函数,但是当我在 if 语句中嵌套结束 cffunction 标记时,它会引发错误。这是代码。

<cffunction name="grabInfo" access="remote" output="false" returntype="any">
  <cfargument name="page" required="yes">
  <cfargument name="pageSize" required="yes">
  <cfargument name="gridsortcolumn" required="yes">
  <cfargument name="gridsortdirection" required="yes">
  <cfargument name="filtercolumn" required="no" default="">
  <cfargument name="filter" required="no" default="">
  <cfargument name="gridID" required="yes">
    <cfif arguments.gridsortcolumn eq "">
      <cfset arguments.gridsortcolumn = "PatientsName" />
      <cfset arguments.gridsortdirection = "asc" />
    </cfif>



<cfif ARGUMENTS.gridID EQ "1">
  <cfquery name="x" datasource="#dsn#">
    <!--- .... --->
  </cfquery>
<cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
</cffunction>
</cfif>

<cfif ARGUMENTS.gridID EQ "2">
<cfquery name="x" datasource="#dsn#">
    <!--- .... --->
  </cfquery>
<cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
</cffunction>
</cfif>

这会给我Context validation error for the cfif tag. 错误,但正如您所见,所有 cfif 语句都已关闭。如果我采用第一个参数并将其与结束 cffunction 标记放在 if 语句之外,它将起作用,就像这样

<cfif ARGUMENTS.gridID EQ "1">
  <cfquery name="x" datasource="#dsn#">
    <!--- .... --->
  </cfquery>
<cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>

</cfif>

<cfif ARGUMENTS.gridID EQ "2">
<cfquery name="x" datasource="#dsn#">
    <!--- .... --->
  </cfquery>
<cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>

</cfif>
</cffunction>

我需要这样做的原因是因为我需要在GridID EQ 2时运行其他几个函数,所以我需要关闭该函数并打开另一个函数,如下所示

<cfif ARGUMENTS.gridID EQ "2">
<cfquery name="x" datasource="#dsn#">
    <!--- .... --->
  </cfquery>
<cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
</cffunction>

<cffunction name="otherFunction">
    <!--- .... --->
</cffunction>
</cfif>

【问题讨论】:

  • 我需要这样做的原因不,你不需要;-)。您混淆了函数声明和用法。组件的功能应该预先定义——不是有条件的,即在cfif 语句中。使用cfif 控制您要调用 的函数以及何时调用。

标签: coldfusion


【解决方案1】:

在您的组件中添加额外的功能。

<cffunction name="grabInfo" access="remote" output="false" returntype="any">
  <cfargument name="page" required="yes">
  <cfargument name="pageSize" required="yes">
  <cfargument name="gridsortcolumn" required="yes">
  <cfargument name="gridsortdirection" required="yes">
  <cfargument name="filtercolumn" required="no" default="">
  <cfargument name="filter" required="no" default="">
  <cfargument name="gridID" required="yes">
  <cfif arguments.gridsortcolumn eq "">
    <cfset arguments.gridsortcolumn = "PatientsName" />
    <cfset arguments.gridsortdirection = "asc" />
  </cfif>

  <cfif ARGUMENTS.gridID EQ "1">
    <cfquery name="x" datasource="#dsn#">
      <!--- .... --->
    </cfquery>
    <cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
  </cfif>

  <cfif ARGUMENTS.gridID EQ "2">
    <cfquery name="x" datasource="#dsn#">
      <!--- .... --->
    </cfquery>
    <!--- call your other functions --->
    <cfset otherFunction(arg1, arg2)>
    <cfset anotherFunction(arg1, arg2)>
    <cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
  </cfif>
</cffunction>

同一组件中的新功能

<cffunction name="otherFunction" access="remote" output="false" returntype="any">
  <cfargument name="arg1">
  <cfargument name="arg2">
  <!--- do things --->
</cffunction>

<cffunction name="anotherFunction" access="remote" output="false" returntype="any">
  <cfargument name="arg1">
  <cfargument name="arg2">
  <!--- do things --->
</cffunction>

【讨论】:

    【解决方案2】:

    您遇到的主要问题是缺乏对代码如何编译的理解。您的代码不会在运行时执行,即在评估条件之类的东西时,需要先编译它。并且要编译,代码需要在语法上有效。你的不是。

    函数是离散的处理单元,需要自包含。从代码的角度来看,您尝试做的事情完全没有意义。它还表明缺乏对函数如何工作的理解。它们在声明时不执行(即:&lt;cffunction&gt;/&lt;/cffunction&gt; 块,它们在被调用时运行。

    Matt 让你走上了正轨,但重申一下,你不要这样做

    <cffunction name="mainFunction">
        <!--- some stuff --->
        <cfif someCondition>
            <!--- some other stuff --->
            <!--- finish off --->
            </cffunction>
        <cfelse>
            <!--- different stuff --->
            <cffunction name="theOtherFunction">
                <!--- different function --->
            </cffunction>
            </cffunction><!--- this is for the outer function --->
        </cfif>
    

    那是……嗯,这是不对的。

    你想要的是这个:

    <cffunction name="mainFunction">
        <!--- some stuff --->
        <cfif someCondition>
            <!--- some other stuff --->
            <!--- different function --->
            <cfset something = theOtherFunction()>
        </cfif>
        <!--- finish off --->
    </cffunction>
    
    <cffunction nname="theOtherFuction">
        <!--- different stuff --->
    </cffunction>
    

    注意每个编码结构是如何自包含的。

    我认为,在深入阅读 CFML 文档以及一些基本的编程教程(任何语言)之前,您可以从中受益。

    另请注意:尽量避免将基于标签的代码用于业务逻辑:标签确实更适合视图,它是对甚至可以定义 带有标签的函数。根据经验:视图标签;逻辑脚本。

    阅读和理解下面的 cmets 也很重要。即使你的代码在语法上是正确的并且可以编译,它仍然不会做你想做的事情,因为函数是与其余代码分开编译的,所以你的条件仍然不起作用。

    【讨论】:

    • 它们在声明时不执行(即:&lt;cffunction&gt;/&lt;/cffunction&gt; 说得好。更进一步,甚至忽略原始代码中的语法错误 - 而 CF可能允许您在 cfif 标记内放置 complete 函数定义,即 &lt;cfif something&gt;&lt;cffunction ...&gt;....&lt;/cffunction&gt;&lt;/cfif&gt; - 您不应该这样做。
    • @Leigh 我没有测试过,但我记得函数声明是与周围的代码分开提取和编译的,所以这样的条件无论如何都是没有意义的。我想我应该重新测试一下。
    • 是的,但 的重点(因为没有更好的词);-) 最初的问题不仅仅是代码必须是“语法上有效的”。 CF 将允许您在条件 .. 中嵌套 complete 函数声明,这可能会导致提问者误解函数的工作原理。尽管 CF 可能会认为这种类型的代码在语法上是有效的,正如您在上面所说的那样 - 它有点毫无意义。这些函数将被提取和预编译,所以它实际上并没有按照他们的想法做。
    • @Leigh:明白了。更新了答案以引起注意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    相关资源
    最近更新 更多