Copy link to clipboard
Copied
I need some clarification about using cfinvoke to call
a function within a component.
I created a component using CFCOMPONENT in a .cfc and inside this component I have a few CFFUNCTIONs.
I'm calling a function from my calling page using cfinvoke
But in my calling page I'm using a loop. My goal is to call that function everytime a record is looping.
So on my calling page I have something like:
<cfquery name="x" datasource ="#mydsn#">
select * from myemp
</cfquery>
<!--- some emp may have more than 1 title --->
<cfset count = 1>
<cfset arr_x = arraynew(1>
<cfloop query="x">
<cfset arr_x[#count#] = "#FN# | #LN# | #MN# | #Title#">
<cfset count = #count + 1>
<cfinvoke component="cfcomponents.mycomp" method="MyMethod" id = "#empId#">
</cfloop>
<cflocation url="someothertemplate.cfm">
What I'm not too clear is:
When I use cf invoke to call MyMethod on each loop, eventhough I did not expect cffunction to return something to the calling page, after MyMethod do what it needs to do, CF will always go back to the calling page for the next record to call MyMethod again until the last record, then once all the record in the query is done looping and done calling MyMethod, CF will process the next tag which is in this case cflocation, am I right?
Copy link to clipboard
Copied
Yes. CF will process all the loop iterations, including calling other code such as your <cfinvoke...>. Then it will move on to the next command after the loop.
But some advice, your are initiating your component each and every iteration of your loop. That is most likely unnecessary overhead. I would suggest you investigate initiating the CFC once before the loop, store the CFC in a variable, then reference that variable in your <cfinvoke....> tag inside the loop so that you don't have to repeatedly initiate the component.
Copy link to clipboard
Copied
In addition to ilssac's answer, I have these observations.
The code you posted shows that you are populating an array but not using it.
When looping through a query, the currentrow variable is already available. You don't have to create another one which you then imcrement.
Copy link to clipboard
Copied
Thank you so much guys!
I'll take a look at the logic again and will do some changes