ColdFusion9 UDF 嵌套调用
ColdFusion9 UDF nested invoke
我有一个在 CF9 中创建的 cfm 文件。它有 7 个 <cfinvoke>
语句,除了 method
和 returnvariable
之外都是相同的。有没有办法将其放入函数或循环中以缩短我的代码并仍然有效?
示例:
<cfsilent>
<cfinvoke component="financial.financial" method="getExecSummary" returnvariable="qExecSummary">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
</cfinvoke>
<!---Added this to test if I can get more than one sheet to the Workbook--->
<cfinvoke component="financial.financial" method="getExecSummary331" returnvariable="qExecSummary331">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
</cfinvoke>
</cfsilent>
这不起作用:
<cffunction name="getSummary" output=true>
<cfargument name="method" required="true">
<cfargument name="returnvariable" required="true">
<cfargument name="level" required="true">
<cfargument name="stateGM" required="true">
<cfinvoke component="financial.financial" method="#method#" returnvariable="#returnvariable#">
<cfinvokeargument name="level" value="#level#" />
<cfinvokeargument name="stateGM" value="#stateGM#" />
</cfinvoke>
<cfreturn #returnvariable#>
</cffunction>
<cfset getSummary("getExecSummary","qExecSummary","#URL.level#","#URL.stateGM#")>
如果有人能指出我正确的方向?如果这可能的话。我一直在尝试查找有关执行此操作的信息,但我还没有看到任何信息。
使用 createObject("component") 比 cfinvoke
更简单。只需创建组件的一个实例。然后调用正确的方法并将结果捕获到所需的变量中:
<!--- separated calls for readability -->
<cfset comp = createObject("component", "path.to.YourComponent")>
<cfset result = comp.firstMethod( "value1", "value2")>
IF 这些方法都是无状态的(并且范围适当)您可以简单地为所有方法调用重用相同的实例:
<cfset comp = createObject("component", "path.to.YourComponent")>
<cfset result1 = comp.firstMethod( "value1", "value2" )>
<cfset result2 = comp.secondMethod( "value1", "value2" )>
<cfset result3 = comp.thirdMethod( "value1", "value2" )>
此外,正如 John Wish :
In CF9+ you can also use the new
operator if you prefer like so:
<cfset comp = new path.to.YourComponent()>
It's worth noting that the new
operator will also try and call an
init method in your CFC if you have one - although it doesn't need one
to work, other than that it works the same as:
createObject("component", "path.to.YourComponent")
我有一个在 CF9 中创建的 cfm 文件。它有 7 个 <cfinvoke>
语句,除了 method
和 returnvariable
之外都是相同的。有没有办法将其放入函数或循环中以缩短我的代码并仍然有效?
示例:
<cfsilent>
<cfinvoke component="financial.financial" method="getExecSummary" returnvariable="qExecSummary">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
</cfinvoke>
<!---Added this to test if I can get more than one sheet to the Workbook--->
<cfinvoke component="financial.financial" method="getExecSummary331" returnvariable="qExecSummary331">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
</cfinvoke>
</cfsilent>
这不起作用:
<cffunction name="getSummary" output=true>
<cfargument name="method" required="true">
<cfargument name="returnvariable" required="true">
<cfargument name="level" required="true">
<cfargument name="stateGM" required="true">
<cfinvoke component="financial.financial" method="#method#" returnvariable="#returnvariable#">
<cfinvokeargument name="level" value="#level#" />
<cfinvokeargument name="stateGM" value="#stateGM#" />
</cfinvoke>
<cfreturn #returnvariable#>
</cffunction>
<cfset getSummary("getExecSummary","qExecSummary","#URL.level#","#URL.stateGM#")>
如果有人能指出我正确的方向?如果这可能的话。我一直在尝试查找有关执行此操作的信息,但我还没有看到任何信息。
使用 createObject("component") 比 cfinvoke
更简单。只需创建组件的一个实例。然后调用正确的方法并将结果捕获到所需的变量中:
<!--- separated calls for readability -->
<cfset comp = createObject("component", "path.to.YourComponent")>
<cfset result = comp.firstMethod( "value1", "value2")>
IF 这些方法都是无状态的(并且范围适当)您可以简单地为所有方法调用重用相同的实例:
<cfset comp = createObject("component", "path.to.YourComponent")>
<cfset result1 = comp.firstMethod( "value1", "value2" )>
<cfset result2 = comp.secondMethod( "value1", "value2" )>
<cfset result3 = comp.thirdMethod( "value1", "value2" )>
此外,正如 John Wish
In CF9+ you can also use the
new
operator if you prefer like so:<cfset comp = new path.to.YourComponent()>
It's worth noting that the
new
operator will also try and call an init method in your CFC if you have one - although it doesn't need one to work, other than that it works the same as:createObject("component", "path.to.YourComponent")