我的cfc中的函数存在问题.当我尝试引入条件逻辑来为网格分配查询时,它们表现得很有趣.基本上在我将拥有的URL中?GRIDID=x它将告诉cfc运行哪个函数,但是当我将结束cffunction标记嵌套在if语句中时,它会抛出错误.这是代码.
<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>
Run Code Online (Sandbox Code Playgroud)
这将给出错误,Context validation error for the cfif tag.但正如您所看到的,所有cfif语句都已关闭.如果我接受第一个参数并将其与cffunctionif语句之外的结束标记放在一起,它就会起作用,就像这样
<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>
Run Code Online (Sandbox Code Playgroud)
我需要这样做的原因是因为我需要在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>
Run Code Online (Sandbox Code Playgroud)
在组件中添加其他功能.
<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>
Run Code Online (Sandbox Code Playgroud)
同一组件中的新功能
<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>
Run Code Online (Sandbox Code Playgroud)
您遇到的主要问题是缺乏对代码编译方式的理解.您的代码不会在运行时执行,即在评估条件之类的东西时,需要先编译它.要编译,代码需要在语法上有效.你的不是.
函数是离散的处理单元,需要自包含.从代码的角度来看,你要做的事情完全没有意义.它还表明缺乏对功能如何运作的理解.它们在声明时不会执行(即:<cffunction>/ </cffunction>block,它们在被调用时运行.
马特让你走上正轨,但重申一下,你不这样做:
<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>
Run Code Online (Sandbox Code Playgroud)
那是......好吧,这不对.
你想要的是这个:
<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>
Run Code Online (Sandbox Code Playgroud)
注意每个编码结构是如何自包含的.
我认为你可以从阅读CFML文档,以及一些基本的编程教程(任何语言)中受益,然后再深入研究它.
另请注意:尽量避免对业务逻辑使用基于标记的代码:标记实际上更适合于视图,并且它可以回溯到早期的,消息不明的时代,甚至可以使用标记定义函数.根据经验:视图标签; 逻辑脚本.
阅读并理解以下评论也很重要.即使你的代码在语法上是正确的并且可以编译,它仍然不会做你想要它做的事情,因为函数是分别编译到代码的其余部分,所以你的条件仍然不起作用.
| 归档时间: |
|
| 查看次数: |
678 次 |
| 最近记录: |