Copy link to clipboard
Copied
I googled for some tutorial on how to work with cfcomponent. I was able to use it in my application but not sure with one area where I need some help.
I used this little javascript (see below) at the bottom of my cf templates especially when I need to continue on my process to other templates
I can't use <CFLOCATION> because in my templates I used <cfflush>
Anyway, my question is not about this javascript, but it is about creating a component object
<script>
window
.location="nameofNextTemplate.cfm";
</script>
Inside of my component, I have the following functions:
<cfcomponent displayname="MyComp">
<CFFUNCTION name="Init" access="public" returntype="any" output="false" hint="Returns an initialized component instance">
<cfreturn THIS />
</CFFUNCTION>
<CFFUNCTION name="Function_1">
...........
</CFFUNCTION>
<CFFUNCTION name="Function_2">
..........
</CFFUNCTION>
<CFFUNCTION name="Function_3">
..........
</CFFUNCTION>
</cfcomponent>
I'm calling Function_1 from A.cfm this way:
<CFSET AddrObject = createObject("component", "cfcomponents.AddrComp").init()>
<CFINVOKE component="#AddrComp#" method="Function_1" param_1="#myparam#">
When codes in A.cfm is done running and if I need to go the B.cfm to continue on processing my next codes, I use the above javascript to navigates to the next cf template.
If I need to call Function_2 from B.cfm, should I do CFSET AddrObject again to initialize the component object again
or I can just do CFINVOKE in B.cfm since AddrObject has been instantiated in A.cfm earlier.
So my confusion has something to do with whether or not I need to keep initializing the component object everytime I need to call Function_1,Function_2,Function_3
from different templates?
Thank you for helping!
 
Copy link to clipboard
Copied
GKiew wrote:
So my confusion has something to do with whether or not I need to keep initializing the component object everytime I need to call
That will depend on what SCOPE you initialize your component into. An initialized component is just a variable, more or less like any other variable and will persist just as long as all the other variables in the same scope.
So to have a component persist from request to request, you just need to initialize it into one of the persistent scopes: i.e. session, application, or server.
For Example:
<CFSET application.AddrObject = createObject("component", "cfcomponents.AddrComp").init()>
This would, in a properly configured application, allow you to invoke methods on that component from any template that shares the same application scope.
NOW I would like to discuss this code.
<script>
window
.location="nameofNextTemplate.cfm";
</script>
WHY OH WHY would you want to send a response to the client browser, just to have the browser bounce back another request for the next step of your processing? I can not imagine why you wouldn't want to wire your code together on the server until ALL the processing is done and just send one response to the client at the end.
Copy link to clipboard
Copied
First, if you create the object first, you don't have to use cfnvoke. You can simply call the function like this:
<cfset YourVar = AddrObject.Function1(param1)>
Cfinvoke is generally used to call methods without creating the objects first.
In your case, cfinvoke might be the way to go. However, if you have several pages accessing the cfc, you may want to run a createobject command and make the object an application variable.
Copy link to clipboard
Copied
Thank you both for the advice
as far as why I'm using the javascript, that's because on this app. I'm doing numerous checking. My app. takes in a huge text feed where I need to check on each
entry for correct format.
So I'm separating the feed into categories, checking each category against the right format and make an update to the feed. When I did not use javascript and have all the checking within CF once, it took so long to process and I got time out error message.
With the javascript, everytime the app. is done checking one category it goes to the next category, etc., this way I don't get the time out error.
I've been searching the solution on how to avoid time out error unsuccessfully
Copy link to clipboard
Copied
GKiew wrote:
I've been searching the solution on how to avoid time out error unsuccessfully
There are several options in ColdFusion.
You could adjust the time-out period with <cfsetting requestTimeOut...> tag or by adjusting the same property in the CF Administrator (though that would affect all CFML code on the server, so is a poor choice for just one piece of code).
But more realistically you may need to break up the processing like you are doing with the JavaScript calls. That could be done with:
<cfhttp....> tags to request the next page. Basically the exact thing you are doing with the JavaScript but requires NO Client interaction.
<cfthread..> with newer versions of CF, a very slick method to break up a large process into discreet parts..
Gateways. Probably overkill for what you are doing, but they also allow one to break up processing for various kinds of tasks.