Coldfusion 放置参数';变量作用域中的结构
我想轻松地将参数的结构内容放入组件所有函数的变量范围中。”“标题”是searchitems结构之一Coldfusion 放置参数';变量作用域中的结构,coldfusion,scope,arguments,cfc,cffunction,Coldfusion,Scope,Arguments,Cfc,Cffunction,我想轻松地将参数的结构内容放入组件所有函数的变量范围中。”“标题”是searchitems结构之一 <cffunction name="setSearch" acces="public" returntype="void"> <cfargument name="searchitems" type="struct" required="true" /> <cfset variables = arguments.searchitems>
<cffunction name="setSearch" acces="public" returntype="void">
<cfargument name="searchitems" type="struct" required="true" />
<cfset variables = arguments.searchitems>
<cfset variables.test = "yo">
</cffunction>
<cffunction name="testit" acces="public" returntype="void">
<cfdump var="#variables.test#"><br>
<cfif isdefined('variables.test')>found in variables.test </cfif>
<cfif isdefined('variables.variables.test')>found in variables.variables.test </cfif>
<hr>
<cfdump var="#variables.title#"><br>
<cfif structkeyexists(variables,'title')>found in variables.title with structkeyexists </cfif>
<cfif structkeyexists(variables.variables,'title')>found in variables.variables.title with structkeyexists</cfif>
<cfif isdefined('variables.title')>found in variables.title </cfif>
<cfif isdefined('variables.variables.title')>found in variables.variables.title</cfif>
</cffunction>
我觉得很奇怪,title可以作为变量转储或输出。title不能用isDefined或structkeyexists检测到。有没有更有效的分配方法
<cfset variables = arguments.searchitems>
使用组件的“此”范围
我建议遵循Adam的建议,将您的
搜索项
保存在变量范围内各自独立的结构中,而不是作为单个项。这样就不会有覆盖其他变量的风险
测试cfc
<cfcomponent>
<cffunction name="init">
<!--- Set up a separate empty container for the searchitems to be available to all functions --->
<cfset variables.searchitems = StructNew()>
<cfreturn this>
</cffunction>
<cffunction name="setSearch" returntype="void">
<cfargument name="searchitems" type="struct" required="true">
<!--- Fill the container with the struct passed into this function --->
<cfset variables.searchitems = arguments.searchitems>
</cffunction>
<cffunction name="dumpSearchTitle" returntype="void">
<cfdump var="#variables.searchitems.title#">
</cffunction>
</cfcomponent>
<cfcomponent>
<cffunction name="init">
<cfreturn this>
</cffunction>
<cffunction name="setSearch" returntype="void">
<cfargument name="searchitems" type="struct" required="true">
<cfset StructAppend( variables,arguments.searchitems,false )>
</cffunction>
<cffunction name="dumpSearchTitle" returntype="void">
<cfdump var="#variables.title#">
</cffunction>
</cfcomponent>
首先也是最重要的是,您需要了解任何对象的私有方法都驻留在variables范围内,并且按照您现在的方式将它们从CFC实例中删除。你可能想把一些东西抽象出来,例如:
variables.searchItems=arguments.searchItems
,这样你就不会随意地把variables范围内的其他东西去掉。稍后将查看您的其余逻辑…最简单的方法是使用属性。有些人可能认为这是编程环境中的一种时尚失礼,但它仍然是最简单的方法。当你设置<代码>变量=参数。SHIVCHIONTION<代码>时,你实际上在变量范围内创建了一个新的结构,称为“变量”,即代码>变量。换句话说,您的变量
结构不会替换真正的变量
范围。当您转储变量.title
CF时,实际上是给您变量.variables.title
。正在推断范围StructKeyExists(variables,“title”)
失败,因为变量范围中没有title
变量(尽管它存在于variables
struct中)。这有什么意义吗?我不会使用this
范围,因为它是公共的,即可以从组件外部更改值。有关最近的解释,请参阅。
<cfcomponent>
<cffunction name="init">
<!--- Set up a separate empty container for the searchitems to be available to all functions --->
<cfset variables.searchitems = StructNew()>
<cfreturn this>
</cffunction>
<cffunction name="setSearch" returntype="void">
<cfargument name="searchitems" type="struct" required="true">
<!--- Fill the container with the struct passed into this function --->
<cfset variables.searchitems = arguments.searchitems>
</cffunction>
<cffunction name="dumpSearchTitle" returntype="void">
<cfdump var="#variables.searchitems.title#">
</cffunction>
</cfcomponent>
<cfscript>
request.searchitems = StructNew();
request.searchitems.title = "mytitletext";
test = CreateObject( "component", "test" );
test.setSearch( request.searchitems );
test.dumpSearchTitle();
</cfscript>
<cfcomponent>
<cffunction name="init">
<cfreturn this>
</cffunction>
<cffunction name="setSearch" returntype="void">
<cfargument name="searchitems" type="struct" required="true">
<cfset StructAppend( variables,arguments.searchitems,false )>
</cffunction>
<cffunction name="dumpSearchTitle" returntype="void">
<cfdump var="#variables.title#">
</cffunction>
</cfcomponent>