Gregory Hill
2006-Apr-10 16:25 UTC
RE: Prototype Ajax - How to pass my own params toonComplete ?
You need a closure.
onComplete: function (response) { NamedActionResponseHandler(theElement,
response); }
Is that what you mean?
Greg
> -----Original Message-----
> From:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-> bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of
Erin Brewer
> Sent: Monday, April 10, 2006 10:18 AM
> To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: [Rails-spinoffs] Prototype Ajax - How to pass my own params
> toonComplete ?
>
> Sorry if this has been covered before, i can''t imagine it
hasn''t, but
> i''ve been unable to find any information on it.
>
> Can i pass my own parameters to the onComplete function specified by
> Ajax.Request ?
>
> My basic setup is i have an html element triggering an event (the ajax
> update).
> I have additional information encoded into the element about how it
> should handle different response codes returned from the ajax event.
> The problem is, i havn''t figured out a way to specify the id of my
> element to be passed along to the onComplete function, so right now i
> just stick it in a global js variable before the ajax call and then
look> it up in my on Complete function, but thats pretty hacky.
>
> I suppose i can pass the elementId as a param to my ajax call, and
have> the responder return it as part of the response codes, but that seems
> kinda weak as well.
>
> Is there no way to keep my current context when the ajax onComplete
> function is triggered?
>
> Here''s my trimmed down code sample:
> -----------------------------------
> //global reference to the element invoking the action
> var NamedActionElement;
>
> function NamedActionHandler(theElement)
> {
> //set our global reference to the element so we can access it in
> later
> functions
> //since ajax calls take us to other functions and we can''t seem
to> pass
> it along
> NamedActionElement = theElement;
>
> //skip setting up my vars for the ajax call
>
> //make the ajax call
> var ajaxCall = new Ajax.Request( urlCall,
> {method: ''post'',
> postBody: postFormData,
> onComplete: NamedActionResponseHandler}
> );
> }
>
> function NamedActionResponseHandler(theResponse)
> {
> var elementThatTriggeredEvent = NamedActionElement;
>
> //do stuff based on values i have encoded into the element and
> //the data returned in theResponse
> }
>
> ----------------------
>
> As you can see, i have to have this global variable so my onComplete
> function can know which element triggered the event.
> Is there a way to pass my own argument to the onComplete function?
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Erin Brewer
2006-Apr-10 19:41 UTC
Re: RE: Prototype Ajax - How to pass my own params toonCompl
Yes, thats exactly what i needed apparently ;-) I had tried to do it as you speced out below, but i got an undefined object js error so i had assumed due to scoping issues i couldn''t quite do it like this. The other response to my post from Michael Peters (which had me to a closure in the actual function invoked by onComplete) seems to work. I tried it again your way (because this seems a little more intuitive to me) but still recieved a js error so i''m not quite sure ... Thanks though. Gregory Hill wrote:> You need a closure. > > onComplete: function (response) { NamedActionResponseHandler(theElement, > response); } > > Is that what you mean? > > Greg > >> -----Original Message----- >> From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-spinoffs- >> Ajax.Request ? >> >> My basic setup is i have an html element triggering an event (the ajax >> update). >> I have additional information encoded into the element about how it >> should handle different response codes returned from the ajax event. >> The problem is, i havn''t figured out a way to specify the id of my >> element to be passed along to the onComplete function, so right now i >> just stick it in a global js variable before the ajax call and then > look >> it up in my on Complete function, but thats pretty hacky. >> >> I suppose i can pass the elementId as a param to my ajax call, and > have >> >> function NamedActionHandler(theElement) >> { >> //set our global reference to the element so we can access it in >> later >> functions >> //since ajax calls take us to other functions and we can''t seem > to-- Posted via http://www.ruby-forum.com/.