質問

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

役に立ちましたか?

解決

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top