Hello everyone, Just a note, I have to pass my JSON in the response body. I''m aware of the X-JSON header, but am passing too much data to use it. No problem and I have code that has it working well. While I''m getting my JSON data back and can do whatever I''d like with it in "setMakeVars", I''d like to set it to a global var "gljson" below, but it never gets set. Everything with the request is fine and there is data in the "json" var in "setMakeVars", but it''s almost like "setMakeVars" doesn''t have access to global vars. var gljson; var url = ''/my/path/here''; var ajobj = new Ajax.Request( url, { method: ''get'', onComplete: setMakeVars }); function setMakeVars (transport) { var json = eval(transport.responseText); gljson = json; } alert(gljson); // is undefined Has anyone else run into this? Any ideas what I can do to fix it? Thanks for any help, Kevin -- Kevin Old kevinold-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
On 6/14/06, Kevin Old <kevinold-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello everyone, > > Just a note, I have to pass my JSON in the response body. I''m aware > of the X-JSON header, but am passing too much data to use it. No > problem and I have code that has it working well. > > While I''m getting my JSON data back and can do whatever I''d like with > it in "setMakeVars", I''d like to set it to a global var "gljson" > below, but it never gets set. > > Everything with the request is fine and there is data in the "json" > var in "setMakeVars", but it''s almost like "setMakeVars" doesn''t have > access to global vars. > > var gljson; > var url = ''/my/path/here''; > var ajobj = new Ajax.Request( > url, > { > method: ''get'', > onComplete: setMakeVars > }); > > function setMakeVars (transport) { > var json = eval(transport.responseText); > gljson = json; > } > > alert(gljson); // is undefined >Hi everyone, I''ve figured out that my variable was being set with the JSON data, but it was only "in scope" with the data inside the setMakeVars function. A friend said that in order to use this data I''d have to make the subsequent calls from within setMakeVars. I''m not sure that''s a good idea. Here''s what I''m trying to do. I''m building a multirecord (in place) edit form and have a need for the Ajax.InPlaceSelect [1,2] lists. It needs the data in two arrays, one for the keys, the other the values. Using the $H() function, I can do this, but since I plan to have 10 records per page (10 instances of the select list), it''d make sense to fetch the data once and store it in a global variable, right? Can this be done? [1] http://dev.rubyonrails.org/attachment/ticket/2667/Ajax.InPlaceSelect.3.js [2] http://wiki.script.aculo.us/scriptaculous/show/InPlaceSelect Any help is greatly appreciated! Kevin -- Kevin Old kevinold-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Gregory Hill
2006-Jun-15 20:47 UTC
RE: Re: Global variables and onComplete in Ajax.Request
> > var gljson; > > var url = ''/my/path/here''; > > var ajobj = new Ajax.Request( > > url, > > { > > method: ''get'', > > onComplete: setMakeVars > > }); > > > > function setMakeVars (transport) { > > var json = eval(transport.responseText); > > gljson = json; > > } > > > > alert(gljson); // is undefined > >That alert is being called before the ajax request finishes (if you told it to send it synchronously instead of asynchronously, it would wait until it finished before it moved on to run the alert). Test it like this: var gljson; var url = ''/my/path/here''; var ajobj = new Ajax.Request( url, { method: ''get'', onComplete: setMakeVars }); function setMakeVars (transport) { var json = eval(transport.responseText); gljson = json; alert(json); testGlobal(); } function testGlobal () { alert(gljson); } That should tell you if it is persisting. The first alert lets you know if the eval worked, then you call another function and alert the global variable to see if it has a value. Greg