开发者

Coldfusion Component Pointers

I'm having an issue with a Coldfusion component that I'm building. Well, I'm not really sure if it's a problem or how things are supposed work, but it seems strange. First, I set up arguments for the init function of my component and invoke the component. But if I change one of the arguments after I invoke, it also changes the values held in the component. Is this correct?

I'm not sure if I'm explaining this very well. Here's some pseudo-code:

<cfset fruit = [] />
<cfset fruit[1] = {
    id = 1,
    name = "apple"
} />
<cfset fruit[2] = {
    id = 2,
    name = "orange"
} />

<cfset args = {
    title = "Fruit",
    data = fruit
} />

<cfinvoke component="test" method="init" returnvariable="test" argumentcollection=#args# />

<cfset fruit[2].name = "banana" />

The init function stores the title and data arguments in the component's variables scope.

Now, if I output the data from the test object, I get apple and banana. But I was expecting the component to retain the data that it 开发者_运维知识库received during the invoke.

Let me know if I need to clarify anything. Thanks for any help!


I'm not really sure if it's a problem or how things are supposed work,

Yes, that is how your current code is supposed to work. Whether that is how you want it to behave is a different question ..

Most complex objects (structures, components, etectera) are passed by reference. Meaning you are essentially just storing a pointer to the object, not the actual values. So you will always see any changes made to the underlying object. That is why the output from test changes.

Arrays are an exception. They are usually (though not always) passed by value. Meaning you have an independent copy that will not reflect any changes made to the original object.

Your example is interesting because it contains both. Changes to your nested structures (name=orange => name=banana) are visible in the component because the structures are passed by reference. But the parent array is not. So you could clear fruit and it would have no effect on the component.

Example:

<cfset fruit[2].name = "bananna" />

<!--- both dumps will show "bananna" --->
<cfdump var="#fruit#" label="Fruit Before" />
<cfdump var="#test.getDataArray()#" label="Test.Data Before" />

<cfset arrayClear(fruit) />

<!--- fruit is empty and the component array is not --->
<cfdump var="#fruit#" label="Fruit After" /> 
<cfdump var="#test.getDataArray()#" label="Test.Data After" /> 

Test:

<cfcomponent>

  <cffunction name="init" returntype="any" ...>
     <cfargument name="title" type="string" required="true" />
     <cfargument name="data" type="array" required="true" />
     <cfset variables.title = arguments.title />
     <cfset variables.data = arguments.data />
     <cfreturn this />   
  </cffunction>

  <cffunction name="getDataArray" returntype="array">
     <cfreturn variables.data />
  </cffunction>

</cfcomponent>

Now, if I output the data from the test object, I get apple and banana.

Right. That is to be expected because structures are passed by reference. If you want the component to maintain a independent copy, you need to duplicate() (ie deep copy) the data element inside init().

   <cfset variables.data = duplicate(arguments.data) />


While Arrays are passed by VALUE in ColdFusion, even the objects inside them. If you want to retain the values in the function return the array that your init function updates and set your fruit array equal to that.

I'd have a getter function that returned your array:

<cffunction name="getArray" returntype="array" access="public" output="false" hint="return the array" >
    <cfreturn variables.theArray>
</cffunction>

And in your cfm template you'd set your fruit array equal to that value:

<cfset fruit = test.getArray()>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜