Thanks, TJ - you're awesome. Yeah - it wasn't a typo - I had a bug in my code.
On Mar 17, 10:06 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote: > @Alphonso77: > > > I know this has to do with variable scope... > > I doubt it. The code you've quoted will (or rather, would) create/ > assign to a global variable provided `myGlobalVariable` isn't declared > as a var within some containing function, which I assume it isn't. > > A couple of thoughts: > > 1. Bug - typo? > > There's a bug in the quoted code: > > function stakeholderModel(strDataNeeded){ > new Ajax.Request('file_name.php'), > // ^--- here > {onSuccess: function(response) { > globalVariable = response.responseXML > }, > parameters: {parm1: parmValue} > }); > > } > > Note the closing parenthesis after your URL, which means that none of > the rest of the stuff is getting passed into the `Ajax.Request` > constructor. You should be seeing a syntax error in your debugger, > because eventually there's a syntax error (the closing parenthesis on > the }); line is unmatched. > > 2. If the bug above is just a typo in your post (*why* do people > retype when there's the beauty of copy and paste??), then I'm guessing > you're thinking of the ajax call as synchronous, e.g.: > > stakeholderModule(someData); > if (myGlobalVariable == somevalue) { > // ... > } > > That won't work, because ajax requests are *asynchronous* by default. > So `stakeholderModule` gets run, which *queues* the request, but the > request doesn't complete until some time later, long after (in > computer terms) you've already checked `myGlobalVariable`. > > It's possible to make ajax requests synchronous (there's an option for > it, see the docs), but it's a very bad idea. Synchronous requests > completely lock up the UI of many browsers for the entire duration of > the request, which leads to a poor user experience. > > Instead, modify your logic so that whatever you were doing after > calling `stakeholderModule` you do from the `onSuccess` function. So > instead of: > > stakeHolderModule(someData); > doThis(); > doThat(); > doTheOther(); > > it's > > stakeHolderModule(someData, function() { > doThis(); > doThat(); > doTheOther(); > > }); > > ...where `stakeholderModule` looks like this: > > function stakeholderModel(strDataNeeded, callback){ > new Ajax.Request('file_name.php', { > onSuccess: function(response) { > globalVariable = response.responseXML; > callback(); > }, > parameters: {parm1: parmValue} > }); > > } > > @Walter: > > > I don't think it's enough to declare it global just by leaving off the > > var keyword when you're already inside an anonymous function. > > It is, provided the symbol isn't in scope (e.g., declared as a var) > anywhere. It's the Horror of Implicit Globals[1], and it's a very bad > idea to rely on it. > > [1]http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html > > HTH, > -- > T.J. Crowder > Independent Software Engineer > tj / crowder software / com > www / crowder software / com > > On Mar 17, 12:16 pm, Walter Lee Davis <wa...@wdstudio.com> wrote: > > > > > > > > > Did you try defining the variable in the global scope first? Something > > like this: > > > var myGlobalVariable > > > function stakeHolderModule(strDataNeeded){ > > new Ajax.Request('foo',{parameters:{bar:baz}, > > onSuccess: function(response){ > > myGlobalVariable = response.responseXML; > > } > > }); > > > } > > > I don't think it's enough to declare it global just by leaving off the > > var keyword when you're already inside an anonymous function. > > > Walter > > > On Mar 16, 2011, at 8:30 AM, Alphonso77 wrote: > > > > I'm trying to either return a value or set a global variable from > > > within a function run on the onSuccess event. I've tried defining an > > > inline function and setting a global variable, returning a value, and > > > even defining a separate function that gets called. The problem is > > > that the variable is getting erased outside of the function holding > > > the new Ajax.request object. > > > > I know this has to do with variable scope, but I'm running out of > > > ideas on how to make the value of my response.responseXML available to > > > functions outside of the one creating the new Ajax.request. Any help > > > you could provide would be greatly appreciated. Here is my code: > > > > function stakeholderModel(strDataNeeded){ > > > new Ajax.Request('file_name.php'), > > > {onSuccess: function(response) { > > > globalVariable = response.responseXML > > > }, > > > parameters: {parm1: parmValue} > > > }); > > > } > > > > -- > > > You received this message because you are subscribed to the Google > > > Groups "Prototype & script.aculo.us" group. > > > To post to this group, send email to > > > prototype-scriptaculous@googlegroups.com > > > . > > > To unsubscribe from this group, send email to > > > prototype-scriptaculous+unsubscr...@googlegroups.com > > > . > > > For more options, visit this group > > > athttp://groups.google.com/group/prototype-scriptaculous?hl=en > > > . -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.