I have a component in Coldfusion8, that constructs a log of error messages and passes it to a variable in JSON to be picked up by Jquery.
I'm rebuilding this component when the user changes languages (reloads the page) like so:
HTML:
<a href="index.cfm?lang=EN">change to english</a>
In the reload I'm checking for lang and if specified am running this in Coldfusion:
<cfif isdefined("lang")>
<cfset Session.lang= #lang#>
<!--- rebuild error messages --->
<cfinvoke component="services.errorMsg"
method="createErrMsgsLog"
returnvariable="errMsgs">
</cfinvoke>
<cfoutput><script type="text/javascript">var errorLog = '<cfoutput>#errMsgs#</cfoutput>';</script></cfoutput>
</cfif>
Question1: Is there a better way to do this? From my little Coldfusion exp, CFINVOKE creates and discards objects, so this should be resource-friendly.
Question2:
How can I run this when the Application/Session starts? I tried running it from my application.cfc onSessionStart but I cannot trigger any Javascript alert/console from there, so I'm afraid nothing happens... Also there must be a better way than to plaster application.cfc with functions like this.
Thanks for some insights!
EDIT:
Halfway there: I can fire this when I change languages:
<cfif isdefined("Sprachwechsel")>
<cfset Session.Sprache = #Sprachwechsel#>
<cfinvoke component="services.errorMsg" method="createErrMsgsLog" returnvariable="Session.errMsgs">
</cfinvoke>
<cfoutput>
<script type="text/javascript">var errorLog = '<cfoutput>#Session.errMsgs#</cfoutput>'</script>
</cfoutput>
However I also want to fire this once from application.cfc. I can use the same code or the code below. Nothing happens.
<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfscript>
APPLICATION.strObjs = structNew();
APPLICATION.strObjs.objErrMsg = createObject("component","services.errorMsg");
</cfscript>
<cfreturn true />
</cffunction>
<cffunction name="onSessionStart" returnType="void" output="false">
<cfscript>
Session.sprache = "DE";
Session.errMsgs = APPLICATION.strObjs.objErrMsg.createErrMsgsLog();
</cfscript>
<cfreturn true />
</cffunction>
I have tried a gazillion variation, but Session.errMsgs just stays undefined if I check for it on the actual page. Any idea what I'm missing?
EDIT2:
Ok. I found the problem reading through here. When I changed my application name, I saw what wasn't working and after fixing this, onSessionStart fired as expected. Quite a ride...