acarpentier-xfEQ3IqfVfY7RlEk0dH8ww@public.gmane.org
2007-Mar-22 18:06 UTC
Ajax.request.responseArgs...
Is it possible with the Ajax object to pass extra parameters that will be accessible on the success callback? Like a responseParameters parameter or something like that?: new Ajax.Request(url, { method:''get'', onSuccess: function(transport){ /* * Here I have some variable or function instance to call... */ functionName(); alert(responseParameters.params[0]); }, responseParameters = new Array(''stuff1'', functionName, ''etc...'') }); On the Yahoo YUI these are called argument in the callback object... wich works great but the YUI is too big for not much, that''s why i want to switch to Prototype: 7 var callback 8 { 9 success: function(o) {/*success handler code*/}, 10 failure: function(o) {/*failure handler code*/}, 11 argument: [argument1, argument2, argument3] 12 } --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
acarpentier-xfEQ3IqfVfY7RlEk0dH8ww@public.gmane.org wrote:> Is it possible with the Ajax object to pass extra parameters that will > be accessible on the success callback? > > Like a responseParameters parameter or something like that?:Just use a closure. This means declaring the var before the anonymous function (the onSuccess callback, or any other callback for that matter) and it will be visible in that anonymous function.> new Ajax.Request(url, > { > method:''get'', > onSuccess: function(transport){ > > /* > * Here I have some variable or function instance to call... > */ > functionName(); > alert(responseParameters.params[0]); > > }, > responseParameters = new Array(''stuff1'', functionName, ''etc...'') > });var responseParameters = [''stuff1'', functionName, ''foo'']; new Ajax.Request(url, { method : ''get'', onSuccess : function(transport) { // call functionName() responseParameters[1](); } } ); -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
acarpentier-xfEQ3IqfVfY7RlEk0dH8ww@public.gmane.org
2007-Mar-22 18:30 UTC
Re: Ajax.request.responseArgs...
Great, but is it safe when using multiple Request? On 22 mar, 14:18, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> acarpent...-xfEQ3IqfVfY7RlEk0dH8ww@public.gmane.org wrote: > > Is it possible with the Ajax object to pass extra parameters that will > > be accessible on the success callback? > > > Like a responseParameters parameter or something like that?: > > Just use a closure. This means declaring the var before the anonymous function > (the onSuccess callback, or any other callback for that matter) and it will be > visible in that anonymous function. > > > new Ajax.Request(url, > > { > > method:''get'', > > onSuccess: function(transport){ > > > /* > > * Here I have some variable or function instance to call... > > */ > > functionName(); > > alert(responseParameters.params[0]); > > > }, > > responseParameters = new Array(''stuff1'', functionName, ''etc...'') > > }); > > var responseParameters = [''stuff1'', functionName, ''foo'']; > new Ajax.Request(url, > { > method : ''get'', > onSuccess : function(transport) { > // call functionName() > responseParameters[1](); > } > } > ); > > -- > Michael Peters > Developer > Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
acarpentier-xfEQ3IqfVfY7RlEk0dH8ww@public.gmane.org wrote:> Great, but is it safe when using multiple Request?Closures capture a reference to the variable. So it will always refer to the same variable. That''s only usually a problem if your variable is in the global scope. If your var is scoped to a previous enclosing sub then it will be fine. -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
acarpentier-xfEQ3IqfVfY7RlEk0dH8ww@public.gmane.org
2007-Mar-22 19:10 UTC
Re: Ajax.request.responseArgs...
Excellent, thanks for support. ;) On 22 mar, 15:06, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> acarpent...-xfEQ3IqfVfY7RlEk0dH8ww@public.gmane.org wrote: > > Great, but is it safe when using multiple Request? > > Closures capture a reference to the variable. So it will always refer to the > same variable. That''s only usually a problem if your variable is in the global > scope. If your var is scoped to a previous enclosing sub then it will be fine. > > -- > Michael Peters > Developer > Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Thursday 22 March 2007 19:06, Michael Peters wrote:> acarpentier-xfEQ3IqfVfY7RlEk0dH8ww@public.gmane.org wrote: > > Great, but is it safe when using multiple Request? >If you''re using closures inside a loop, there is a second gotcha to watch out for, if you''re enclosing a variable that gets modified in subsequent passes of the loop. I wrote up a short example of this a while back, here: http://dave.sunwheeltech.com/wordpress/2006/04/30/more-closure-strangeness/ Best way to avoid this is to get into Prototype''s array methods, as the iterator function will isolate each pass of the loop into a separate execution context automatically. Dave> Closures capture a reference to the variable. So it will always refer to > the same variable. That''s only usually a problem if your variable is in the > global scope. If your var is scoped to a previous enclosing sub then it > will be fine.-- ---------------------- Author Ajax in Action http://manning.com/crane Ajax in Practice http://manning.com/crane2 Prototype & Scriptaculous in Action http://manning.com/crane3 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---