I’m having a multi-language site and need to pass error messages in different languages to the browser for client-side form validation.
Right now I’m using a error.cfc, which contains:
<cfcomponent displayname="errorMsg" hint="create error msgs">
<cffunction name="createErrMsgsLog" output="false" returntype="string">
<cfset var allErrMsgs=ArrayNew(2)>
<cfquery datasource="db" name="texte" cachedWithin="#Session.globalCache#">
... query db for snippets and text blocks ...
</cfquery>
<cfoutput query="texte"><cfset "#trim(snippet)#" = "#trim(text)#"></cfoutput>
<cfscript>
allErrMsgs[1][1] = "comp";
allErrMsgs[1][2] = tx_validate_comp;
...
</cfscript>
<cfset Session.errMsgs = serializeJSON(allErrMsgs)>
<cfreturn Session.errMsgs>
</cffunction>
</cfcomponent>
So I’m grabbing all texts, fill an array, which is serialized and returned as a string.
The cfc is called inside application.cfc
<cffunction name="onSessionStart" returnType="boolean" output="false" hint="session initalizer">
...
<cfinvoke component="services.errorMsg" method="createErrMsgsLog" returnvariable="errMsgs"></cfinvoke>
<cfset Session.errMsgs = errMsgs>
...
</cffunction>
And on every page I’m including it like this:
<script type="text/javascript" language="JavaScript">var #toScript(Session.errMsgs, "errorLog")#</script>
I’m not sure why I’m ending up with errorLog is undefined sometimes, but it happens occassionaly when I’m refreshing a page or when I enter a page URL manually inside my application (go from page ABC to page DEF).
I’m using Jquery Mobile, so when the Session inits and the first page loads, the errorLog is rendered once into the first page. Since all subsequent pages are pulled in/removed from this page, the initial errorLog is always active. I guess when manually changing the URL, I’m loosing the first page errorLog, because I’m loading a new page, but I don’t understand why the new page does not get “it’s own” errorLog or why it’s undefined all of a sudden.
Question:
Any idea how I can make sure errorLog is defined = how can I ensure the errorMessages gets created in Coldfusion?
Thanks for help!
EDIT:
Ok. I have added a check in Coldfusion, so in case the Session variables is undefined, I’m recreating it. So far however, it is always there. It seems to only happen when I manually change the URL and go to another page. I can reproduce in IE8. Strangely, when I check the IE8 debugger, errorLog is there, but I’m still getting an error telling me it’s undefined…
EDIT:
I have moved the cfinvoke to on request handler inside the application.cfc.
<cfif len( Session.errMsgs ) EQ 0>
<cfinvoke component="services.errorMsg" method="createErrMsgsLog" returnvariable="errMsgs"></cfinvoke>
<cfset Session.errMsgs = errMsgs>
</cfif>
The Coldfusion part seems to work correctly, because I’m only ending up inside the if-statement when the application fires up. Afterwards errMsgs are defined, but the problem remains… Frustrating.
EDIT:
Another cause could be my JSON string. I’m setting it like this:
<script type="text/javascript">var #toScript(Session.errMsgs, "errorLog")#; alert(errorLog);</script>
The alert alerts it allright, but if I try to grab it later on like so:
err = jQuery.parseJSON(errorLog);
I’m getting NULL. My JSON looks like this (valid, alerting Session.errMsgs)
[["firma","Bitte Firmennamen eingeben!"],["firma_re","Bitte Firmennamen eingeben!"],...
or this (invalid, outputting with toScript):
"[[\"firma\",\"Bitte Firmennamen eingeben!\"],[\"firma_re\",\.... (invalid)
I think I found the issue. I’m using RequireJS and I had the call to initiliaze requireJS above the snippet, which entered the variable onto the page. While this seems to work on the first page, on subsequent page, requireJS seems much faster in re-parsing js files, so the file looking for errorLog was parsed before the variable was set on the page. Moving requireJS init to after setting errorLog seems to solve the problem…. some search…