Hello together, I''m trying to achieve something like a buffered function callback (don''t know how to describe it better). Let me explain the reasoning behind this, I think it will become clear if I do so. I''m using Ajax.Responders.register determine wether a timeout on the user''s side has been reached. A timeout happens if there was too much time between to actions. The called PHP page then throws a 403 header (''forbidden''). Ajax.Responders.register({ onComplete: function(oReq, oJSN) { // Continue on success if (oReq.success()) { this.onSuccess(); } // Else error handling else { var status = oJSN.status; // Timeout if (status == 403) { showRelog(); } else { // something else } } } }); function showRelog() { // Show a relog div with username and password field } I have a link which calls some other function: <div id=''clickme'' onclick=''doStuff();''>Click me for some action</div> function doStuff() { var x = $(''someOtherElement'').firstChild; var y = 1234; var toURL = ''myphppage.php''; var params = { one: x, two: y }; new Ajax.Request( toURL, { method: ''post'', parameters: params, onSuccess: function(transport) { // Do something } }); } What happens now if a user has spent too much time between two actions: - He clicks on ''clickme'' and therefore calls the function ''doStuff()''. - myphppage.php compares the current timestamp with a saved value (''lastclick'') and adds a given timeout value (say, 60 seconds). If it notices that too much time has passed, it outputs a 403 header. - Through the Ajax.Responders, the script executes ''showRelog()'' which asks the user to input his username and password. - If the enters them correctly, another PHP page is called which renews the lastclick variable, and he can proceed as normal. However, what I want to achieve is that the function ''doStuff()'' is executed after a successful re-login. Currently this function is omitted, due to the Ajax.Responders, which aborts the whole process. Is there a way to somehow buffer the called function and execute it after afterwards? Thank you, Robert --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Robert, I''m not sure how you''d approach this with a responder, but you don''t really need one and you may be better off just using a named function instead of a closure for your onComplete handler and passing it into each call. That way, the function doing the request can pass a reference to itself (and its parameters) to the onComplete handler (via a closure). The onComplete handler can then pass them to the reLog function to be executed when the re-login is successfully completed. I don''t have time right now to dash off an example, but if you take your current onComplete handler and make it a named function requestComplete, you can give it two extra args: fung and args. So doStuff''s request would be : function doStuff(a, b, c) { new Ajax.Request(someurl, { onComplete: function (transport) { requestComplete(transport, arguments.callee, $A(arguments)); } }); } Each of your retry-able functions would use the same pattern. requestComplete would pass on the function and arguments to reLog, which would (probably in a special completion handler) ultimately call them like this: func.apply(undefined, args); Naturally, if you have context you want to pass along, you can do that as well and then specify the context as the first parameter to apply. Hope this helps, -- T.J. Crowder tj / crowder software / com On Jun 23, 3:57 pm, Robert <spoon...-hi6Y0CQ0nG0@public.gmane.org> wrote:> Hello together, > > I''m trying to achieve something like a buffered function callback > (don''t know how to describe it better). > Let me explain the reasoning behind this, I think it will become clear > if I do so. > > I''m using Ajax.Responders.register determine wether a timeout on the > user''s side has been reached. > A timeout happens if there was too much time between to actions. The > called PHP page then throws a 403 header (''forbidden''). > > Ajax.Responders.register({ > onComplete: function(oReq, oJSN) > { > // Continue on success > if (oReq.success()) > { > this.onSuccess(); > } > // Else error handling > else > { > var status = oJSN.status; > > // Timeout > if (status == 403) > { > showRelog(); > } > else > { > // something else > } > > } > } > > }); > > function showRelog() > { > // Show a relog div with username and password field > > } > > I have a link which calls some other function: > <div id=''clickme'' onclick=''doStuff();''>Click me for some action</div> > > function doStuff() > { > var x = $(''someOtherElement'').firstChild; > var y = 1234; > > var toURL = ''myphppage.php''; > var params = { one: x, two: y }; > > new Ajax.Request( > toURL, > { > method: ''post'', > parameters: params, > onSuccess: function(transport) > { > // Do something > } > }); > > } > > What happens now if a user has spent too much time between two > actions: > - He clicks on ''clickme'' and therefore calls the function ''doStuff()''. > - myphppage.php compares the current timestamp with a saved value > (''lastclick'') and adds a given timeout value (say, 60 seconds). If it > notices that too much time has passed, it outputs a 403 header. > - Through the Ajax.Responders, the script executes ''showRelog()'' which > asks the user to input his username and password. > - If the enters them correctly, another PHP page is called which > renews the lastclick variable, and he can proceed as normal. > > However, what I want to achieve is that the function ''doStuff()'' is > executed after a successful re-login. Currently this function is > omitted, due to the Ajax.Responders, which aborts the whole process. > > Is there a way to somehow buffer the called function and execute it > after afterwards? > > Thank you, > > Robert--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
We have implemented exactly this type of behavior in our system, but not with responders. The basic idea is to follow this flow for each request: 1. if logged in (i.e. timeout has not occured), just make the Ajax call, passing the passed in callback function directly 2. if logged out, wrap the passed in callback function inside another function that will make the desired call, with current callback as ITS callback (i.e. nest the current callback within another anonymous function as the callback for the desired call). With that new callback function defined, pass IT in to the login process, so that once login is succesful, it will make the desired Ajax call with the original callback method as the ultimate callback. It''s hard to explain because of the nesting... but hopefully that gets you further down your path. Here''s pseudo-code: function makeAJAXCall(callback) { if (loggedIn) { Ajax.request("someServerMethod", callback); } else { var newCallback = function() { Ajax.request("someServerMethod", callback); }; doTheLoginStuff(newCallback); } } On Mon, Jun 23, 2008 at 4:00 PM, T.J. Crowder <tj-MUGVhKWuB3Yd9SLi6J12IkEOCMrvLtNR@public.gmane.org> wrote:> > Hi Robert, > > I''m not sure how you''d approach this with a responder, but you don''t > really need one and you may be better off just using a named function > instead of a closure for your onComplete handler and passing it into > each call. That way, the function doing the request can pass a > reference to itself (and its parameters) to the onComplete handler > (via a closure). The onComplete handler can then pass them to the > reLog function to be executed when the re-login is successfully > completed. > > I don''t have time right now to dash off an example, but if you take > your current onComplete handler and make it a named function > requestComplete, you can give it two extra args: fung and args. So > doStuff''s request would be : > > function doStuff(a, b, c) { > new Ajax.Request(someurl, { > onComplete: function (transport) { > requestComplete(transport, arguments.callee, > $A(arguments)); > } > }); > } > > Each of your retry-able functions would use the same pattern. > requestComplete would pass on the function and arguments to reLog, > which would (probably in a special completion handler) ultimately call > them like this: > > func.apply(undefined, args); > > Naturally, if you have context you want to pass along, you can do that > as well and then specify the context as the first parameter to apply. > > Hope this helps, > -- > T.J. Crowder > tj / crowder software / com > > > On Jun 23, 3:57 pm, Robert <spoon...-hi6Y0CQ0nG0@public.gmane.org> wrote: > > Hello together, > > > > I''m trying to achieve something like a buffered function callback > > (don''t know how to describe it better). > > Let me explain the reasoning behind this, I think it will become clear > > if I do so. > > > > I''m using Ajax.Responders.register determine wether a timeout on the > > user''s side has been reached. > > A timeout happens if there was too much time between to actions. The > > called PHP page then throws a 403 header (''forbidden''). > > > > Ajax.Responders.register({ > > onComplete: function(oReq, oJSN) > > { > > // Continue on success > > if (oReq.success()) > > { > > this.onSuccess(); > > } > > // Else error handling > > else > > { > > var status = oJSN.status; > > > > // Timeout > > if (status == 403) > > { > > showRelog(); > > } > > else > > { > > // something else > > } > > > > } > > } > > > > }); > > > > function showRelog() > > { > > // Show a relog div with username and password field > > > > } > > > > I have a link which calls some other function: > > <div id=''clickme'' onclick=''doStuff();''>Click me for some action</div> > > > > function doStuff() > > { > > var x = $(''someOtherElement'').firstChild; > > var y = 1234; > > > > var toURL = ''myphppage.php''; > > var params = { one: x, two: y }; > > > > new Ajax.Request( > > toURL, > > { > > method: ''post'', > > parameters: params, > > onSuccess: function(transport) > > { > > // Do something > > } > > }); > > > > } > > > > What happens now if a user has spent too much time between two > > actions: > > - He clicks on ''clickme'' and therefore calls the function ''doStuff()''. > > - myphppage.php compares the current timestamp with a saved value > > (''lastclick'') and adds a given timeout value (say, 60 seconds). If it > > notices that too much time has passed, it outputs a 403 header. > > - Through the Ajax.Responders, the script executes ''showRelog()'' which > > asks the user to input his username and password. > > - If the enters them correctly, another PHP page is called which > > renews the lastclick variable, and he can proceed as normal. > > > > However, what I want to achieve is that the function ''doStuff()'' is > > executed after a successful re-login. Currently this function is > > omitted, due to the Ajax.Responders, which aborts the whole process. > > > > Is there a way to somehow buffer the called function and execute it > > after afterwards? > > > > Thank you, > > > > Robert > > >-- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- WebWidgetry.com / MashupStudio.com Future Home of the World''s First Complete Web Platform -- Inquire: 1-920-574-2218 Blog: http://www.someElement.com LinkedIn Profile: http://www.linkedin.com/in/ryangahl --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---