As you said you wanted to simplify your code, I'd recommend not using
global vars. You might want to use the same var somewhere else in the
code and create a mess. Why not just pass it to the function that will
use it?

  if (response.MESSAGE == 'Success' ){
          fnCreateStorySuccessResponse(response.TITLE);
  }

function fnCreateStorySuccessResponse(title){
   $('#newStoryTitleSPAN').text(title);
};

In my opinion this 'tight coupling' is much easier to handle and
debug. A global var can be useful sometimes, but when you're setting
it from an Ajax call you never know when it's gonna be available.

cheers,
- ricardo

On Apr 6, 3:27 pm, "Michael Geary" <m...@mg.to> wrote:
> I probably shouldn't have suggested using alert() since it actually can
> affect the order of execution in an odd way. (The code immediately below the
> alert call won't execute until you close the alert, but other code in the
> page could execute during that time in some cases.)
>
> So stick with console.log() for this kind of tracing. I would just start
> putting console.log('xyz',gNewStoryTITLE) calls before/after every line of
> code (where you give each one a different 'xyz' to distinguish them), and
> see exactly where it goes wrong.
>
>   _____  
>
> From: Rick Faircloth
>
> This is part of a content management system for a client, so it's not
>
> publicly accessible.if it appears I can't solve the problem otherwise,
>
> I'll set up a test case based on the code that is publicly accessible.
>
> However, here's the relevant code from the app that shows what I'm
>
> doing:  (and just so you're not confused, I've swapped from using
>
> the gNewStoryID variable to gNewStoryTITLE variable, but the usage
>
> is exactly the same)
>
> From the AJAX success section in which the global variable is set:
>
> success:  function(response) {
>
>                         if (response.MESSAGE == 'Success' )
>
>                             {gNewStoryTITLE = response.TITLE;
>
>                               alert(gNewStoryTITLE);    <--- variable shows
> up here with correct value
>
>                               fnCreateStorySuccessResponse();}
>
> So you can see that the fn in which I'm going to use the new global variable
>
> is being called *after* the global var is established, in
> fnCreateStorySuccessResponse().
>
> fnCreateStorySuccessResponse consists of this code:
>
> (it has more lines than below, but they are irrelevant and don't involve the
> variable)
>
> $('#newStoryTitleSPAN').empty().append(gNewStoryTITLE);
>
> The gNewStoryTitle variable shows up in the specified Span correctly, but
> then firebug
>
> immediately throws and "gNewStoryTITLE is not defined" error.
>
> Actually, from observing the sequence of events on the screen, even after
> removing the
>
> alert(gNewStoryTITLE); from the sequence, it almost appears as if the
>
> sequence of events is firing out of sequence that is specified above.
>
> The error message is actually showing up in firebug before the
> gNewStoryTitle value
>
> gets displayed on-screen in the span, which happens when
> fnCreateStorySuccessResponse()
>
> is triggered.
>
> It's as if fnCreateStorySuccessResponse() tries to use the variable before
> it is set, then
>
> after the error message is displayed, the variable is finally set and
>
> properly displayed on-screen.  But that doesn't make sense.
>
> Shouldn't the order of statements in the success: section of the ajax
> fuction occur
>
> sequentially?
>
> Rick
>
> From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
> Behalf Of Michael Geary
> Sent: Monday, April 06, 2009 1:37 PM
> To: jquery-en@googlegroups.com
> Subject: [jQuery] Re: Trying to understand how to use global variables...
>
> It would be easier if you had a link to a test page or at least some code to
> look at. Otherwise we can only guess what might be wrong.
>
> Here is one possibility.
>
> Note what you said here:
>
> > ...by using gNewStoryID = response.STORY_ID, I was creating a
> > global variable that could then be used in any function.
>
> (emphasis added)
>
> You are exactly right: First you create the global variable with the
> assignment statement, and then you can use it.
>
> The question is, when is then?
>
> The global variable is created when your callback function executes.
>
> These other functions where you're trying to use it: Are they being called
> before the callback function executes, or after? If you put alert() or
> console.log() calls wherever you set or reference your global variable, what
> order do they appear in?
>
> -Mike
>
>   _____  
>
> From: Rick Faircloth
>
> I've read up on the subject, but my application is showing that
>
> my understanding has gaps.
>
> If I have an ajax function that in the returned "response" exists
>
> a value called "response.STORY_ID" and I used the statement in the
>
> success section of the ajax function, gNewStoryID = response.STORY_ID,
>
> and then alert(gNewStoryID), I do get the property ID returned in the alert.
>
> Now I want to use that variable, gNewStoryID, in other functions.  My
> understanding
>
> was that, by using gNewStoryID = response.STORY_ID, I was creating a global
> variable
>
> that could then be used in any function.
>
> However, when I try to use the gNewStoryID in another function, for
> instance,
>
> $('#newStoryIDSpan').empty().append(gNewStoryID);
>
> I get an error stating "gNewstoryID is not defined".
>
> Where is my understanding in complete?  I was think that a global javascript
> variable
>
> would be available through the application as is a ColdFusion session
> variable.
>
> Thanks for helping me understand!
>
> Rick
>
> ----------------------------------------------------------------------------
> -----------------------------------------------------------
>
> "It has been my experience that most bad government is the result of too
> much government." - Thomas Jefferson

Reply via email to