david.dSingleton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-15 20:45 UTC
Prototype timing issues!
I''ve been having some very frustrating issues with Ajax.Request, or prehaps its more in line to say that my problem is with my knowledge of javascript. here is what is happening. I am using a windowed login template for our internal site, where certain things requires a supervisor password, and clock number, i pull these in from our mysql database. However, my script keeps going without waiting for my result to get back. Here is the page where i got my window template: http://prototype-window.xilinus.com/ It works great, got it set up fine, I''ve even edited the code to go check to see if my password is valid. My response page returns either "true", or "false", but I get the screenshake and stuff before my request is finished, can anyone give me a hand here? Here is my function. <code> function ReqPW(dept, level) { var res = ''''; Dialog.confirm($(''login'').innerHTML, { className:"alphacube", width:400, okLabel: "login", cancelLabel: "cancel", onOk:function(win) { var clkno = $F(''clockno''); var pw = $F(''pswd''); var deptno = dept; var lvl = level; var params = ''ClockNo='' + clkno + ''&Dept='' + deptno + ''&passwd='' + pw + ''&Level='' + lvl; var url = ''../Processes/leadlogin.lasso?'' + params; new Ajax.Request(url, { method: ''get'', onComplete: function(tryitout) { var res = tryitout.responseText; } }); if(res.match(''true'')) { return true; } else { $(''login_error_msg'').innerHTML=''Login or password inccorect''; $(''login_error_msg'').show(); Windows.focusedWindow.updateHeight(); new Effect.Shake(Windows.focusedWindow.getId()); return false; } } }); } </code> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
You need to put your entire "if(res.match(''true'')" block inside the onComplete function... On 3/15/07, david.dSingleton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <david.dSingleton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''ve been having some very frustrating issues with Ajax.Request, or > prehaps its more in line to say that my problem is with my knowledge > of javascript. here is what is happening. > > I am using a windowed login template for our internal site, where > certain things requires a supervisor password, and clock number, i > pull these in from our mysql database. However, my script keeps going > without waiting for my result to get back. > > Here is the page where i got my window template: > > http://prototype-window.xilinus.com/ > > It works great, got it set up fine, I''ve even edited the code to go > check to see if my password is valid. My response page returns either > "true", or "false", but I get the screenshake and stuff before my > request is finished, can anyone give me a hand here? > > Here is my function. > > <code> > function ReqPW(dept, level) { > var res = ''''; > > Dialog.confirm($(''login'').innerHTML, > { > className:"alphacube", > width:400, > okLabel: "login", > cancelLabel: "cancel", > onOk:function(win) > { > var clkno = $F(''clockno''); > var pw = $F(''pswd''); > var deptno = dept; > var lvl = level; > var params = ''ClockNo='' + > clkno + ''&Dept='' + deptno + ''&passwd='' > + pw + ''&Level='' + lvl; > var url > ''../Processes/leadlogin.lasso?'' + params; > > new Ajax.Request(url, > { > method: > ''get'', > onComplete: > function(tryitout) > { > var > res = tryitout.responseText; > } > }); > if(res.match(''true'')) > { > return true; > } > else > { > $(''login_error_msg'').innerHTML=''Login > or password inccorect''; > > $(''login_error_msg'').show(); > > Windows.focusedWindow.updateHeight(); > new Effect.Shake( > Windows.focusedWindow.getId()); > return false; > } > } > }); > > } > > </code> > > > > >-- Ryan Gahl Application Development Consultant Athena Group, Inc. Inquire: 1-920-955-1457 Blog: http://www.someElement.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan, I have tried that also and that doesn''t fix the issue. Then, my Ajax.Request returns true or false, but not the OnOk function. And, anytime I try and put something after the close of my Ajax.Request, it executes before my Ajax Request is returned. On 3/15/07, Ryan Gahl <ryan.gahl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You need to put your entire "if(res.match(''true'')" block inside the > onComplete function... > > > > On 3/15/07, david.dSingleton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <david.dSingleton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''ve been having some very frustrating issues with Ajax.Request, or > > prehaps its more in line to say that my problem is with my knowledge > > of javascript. here is what is happening. > > > > I am using a windowed login template for our internal site, where > > certain things requires a supervisor password, and clock number, i > > pull these in from our mysql database. However, my script keeps going > > without waiting for my result to get back. > > > > Here is the page where i got my window template: > > > > http://prototype-window.xilinus.com/ > > > > It works great, got it set up fine, I''ve even edited the code to go > > check to see if my password is valid. My response page returns either > > "true", or "false", but I get the screenshake and stuff before my > > request is finished, can anyone give me a hand here? > > > > Here is my function. > > > > <code> > > function ReqPW(dept, level) { > > var res = ''''; > > > > Dialog.confirm($(''login'').innerHTML, > > { > > className:"alphacube", > > width:400, > > okLabel: "login", > > cancelLabel: "cancel", > > onOk:function(win) > > { > > var clkno > > $F(''clockno''); > > var pw = $F(''pswd''); > > var deptno = dept; > > var lvl = level; > > var params = ''ClockNo='' > > + clkno + ''&Dept='' + deptno + ''&passwd='' > > + pw + ''&Level='' + lvl; > > var url > > ''../Processes/leadlogin.lasso?'' + params; > > > > new Ajax.Request(url, > > { > > method: > > ''get'', > > onComplete: > > function(tryitout) > > { > > > > var > > res = tryitout.responseText; > > > > } > > }); > > if(res.match(''true'')) > > { > > return true; > > } > > else > > { > > $(''login_error_msg'').innerHTML=''Login > > or password inccorect''; > > > > $(''login_error_msg'').show(); > > > > Windows.focusedWindow.updateHeight(); > > new Effect.Shake > > (Windows.focusedWindow.getId()); > > return false; > > } > > } > > }); > > > > } > > > > </code> > > > > > > http://www.someElement.com > > > > > >-- Thank you, David Singleton --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ok, looks like you need a little primer on the whole asynchronous thing. The onComplete function you pass in to an Ajax.request call gets executed later (when the ajax call returns from the server). So in your code when you call the Ajax.request, you are then right away expecting the variable to hold relevant information, but the onComplete function hasn''t been executed yet... You''re just kind of missing the whole asynchronous thing (which is something we all had to learn to get used to as well). Now as for the problem... You''re right, I kind of overlooked the fact that you had all this in a function that was expecting to return a value from this call, so while my suggestion would allow you to correctly evaluate the value that came from the server, again the outer body of the onOK function will have already been evaluated. Again, you need to think about the order of operations, and how this code is actually going to be executed, keeping in mind that the code keeps right along executing after you call Ajax.request. By default Ajax.request operates asynchronously, so that means your code is not going to sit there and wait for the call to come back for the server. By the time your call comes back from the server, and your onComplete function executes, all the other code will have been evaluated. So really the only way to keep your code written how it is would be to make your Ajax call a synchronous one, otherwise you have to take other measures, and certainly rethink what''s actually going on. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---