1

我想轻松地将参数的结构内容放入组件所有功能的变量范围内。'Title' 是 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>

但是运行它会给出:

yo
found in variables.test

mytitletext
found in variables.variables.title with structkeyexists
found in variables.variables.title 

我觉得很奇怪,标题可以作为 variables.title 转储或输出,但不能用 isDefined 或 structkeyexists 检测到。有没有更有效的分配方式

<cfset variables = arguments.searchitems> 
4

2 回答 2

2

使用组件的“this”范围。

<cfcomponent>

<cfset this.myArgs = StructNew()>
<cfset this.test = "">


<cffunction name="setSearch" acces="public" returntype="void">    
     <cfargument name="searchitems" type="struct" required="true" />
     <cfset this.myArgs= arguments>
     <cfset this.test = "yo">           
</cffunction>

</cfcomponent>
于 2013-09-12T10:29:34.520 回答
1

我建议您遵循 Adam 的建议,并searchitems在变量范围内保留您自己的单独结构,而不是作为单独的项目。这样你就不会冒险覆盖其他变量。

测试.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>

索引.cfm

<cfscript>
    request.searchitems = StructNew();
    request.searchitems.title = "mytitletext";
    test = CreateObject( "component", "test" );
    test.setSearch( request.searchitems );
    test.dumpSearchTitle();
</cfscript>

但是,如果将个人放在变量范围内真的很重要searchitems,那么您可以将它们附加到变量范围内。第三,false参数 ofStructAppend确保您不会覆盖现有变量。

测试.cfc

<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>
于 2013-09-14T08:36:50.357 回答