1

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...

frequent
  • 27,643
  • 59
  • 181
  • 333

2 Answers2

1

There probably is a better way to do it, but from the code you posted it is hard to tell which way is best for your particular situation. I assume your code sample is in your index.cfm?

Here's what you might do:

1) in your onApplicationStart() method in Application.cfc cache your component:

application.objErrMsh = createobject("component", "services.errorMsg");

2) in onSessionStart() in your Application.cfc set session.lang to your default language and set session.errMsg var:

session.lang = "DE";
session.errMsg = application.objErrMsg.createErrMsgsLog(session.lang);

(I assume here that your component's return depends on user's language setting. If not - what is the point in re-invoking the component on language change?)

3) whenever you need to set your javascript errorLog var you can now do:

<script type="text/javascript">var errorLog = <cfoutput>#session.errMsg#</cfoutput>;</script>

4) when a user clicks on your 'change language' link, run this code:

<cfparam name="url.lang" type="string" default="DE">
<cfif url.lang neq session.lang>
  <cflock scope="session" type="exclusive">
  <cfset session.lang = url.lang>
  <cfset session.errMsg = application.objErrMsg.createErrMsgsLog(session.lang)>
  </cflock>
</cfif>

I.e. the above code will be at the top of your index.cfm page. You will also want to add some validation for url.lang value (e.g. that it is in the list of accepted values) so you don't end up with all sorts of junk in there - always sanitize all user-supplied values.

hth

azawaza
  • 3,065
  • 1
  • 17
  • 20
  • Cool. Looking into this. I had set the default language as a session variable. I thought about the errorMsg, too, but then put it outside of onSessionStart, because I was afraid it wouldn't update when the user switches languages. But your sample takes care of this. Values in error messages are all by me. Still, Thanks for the sanitation tip. Keeping this in mind. – frequent Apr 29 '12 at 18:57
  • sorry, that should have been _createErrMsgsLog_ (I missed the last _s_ in Msgs) - the name of the method in your errorMsg.cfc. I have edited the code in my answer to correct the typo. – azawaza Apr 30 '12 at 03:09
  • no problem. Thanks for the help so far. I can get the .cfc to run when I change lanuages, but I can absolutely not fire it from onSessionStart or onApplicationStart. Nothing happens. I'm posting my code above. Feel free to chip in with some more input. Too bad I can only do one upvote. Thanks again! – frequent Apr 30 '12 at 17:43
1

I can't help much with question 1, but maybe can with 2.

You should be able to put your code in a CFC somewhere, and run it from the Application.cfc (and please do make sure that the file name starts with a capital A), onRequestStart method.

Barry
  • 432
  • 5
  • 9
  • 1
    It's a best practice, and most importantly, without the A, the ColdFusion server won't find it in some environments, like Unix. Where I work, we used to use Windows IIS servers, and a lot of developers got in the habit of using all lower case, then we switched to Unix Apache servers. It wasn't fun. ColdFusion doesn't complain if it doesn't find an Application.cfc or Application.cfm file, but applications do complain a lot when variables aren't set. A little adherence to best practices up front would have saved us quite a few problems. – Barry Apr 29 '12 at 19:00