Erin Brewer
2006-Apr-10 16:17 UTC
Prototype Ajax - How to pass my own params to onComplete ?
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/.
Michael Peters
2006-Apr-10 16:21 UTC
Re: Prototype Ajax - How to pass my own params to onComplete ?
Erin Brewer wrote: [snip]> Is there no way to keep my current context when the ajax onComplete > function is triggered?The easiest way is to just use a closure.> 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 > }change this function to return another function. Something like this: function NamedActionResponseHandler(NamedActionElement, etc) { return function(theResponse) { //do stuff based on values i have encoded into the element and //the data returned in theResponse } } And then call it like this: onComplete: NamedActionResponseHandler(theElement), -- Michael Peters Developer Plus Three, LP
Erin Brewer
2006-Apr-10 17:22 UTC
Re: Prototype Ajax - How to pass my own params to onComplete
Cool, that works nicely. I''ve never actually come across the term ''closure'' before in JS. Did a quick search and read a couple things (still don''t quite understand exactly how that works to get the response reference?), but from my reading, should i do anything after the clousre to avoid memory leaks? This same function flow may be invoked multiple times per page depending on the nature of the event (event1 does ajax stuff, response triggers event2, etc.) Thanks again. Michael Peters wrote:> Erin Brewer wrote: > [snip] >> Is there no way to keep my current context when the ajax onComplete >> function is triggered? > > The easiest way is to just use a closure. > >> //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 >> } > > change this function to return another function. Something like this: > > function NamedActionResponseHandler(NamedActionElement, etc) { > > return function(theResponse) { > //do stuff based on values i have encoded into the element and > //the data returned in theResponse > } > } > > And then call it like this: > onComplete: NamedActionResponseHandler(theElement), > > -- > Michael Peters > Developer > Plus Three, LP-- Posted via http://www.ruby-forum.com/.
Michael Peters
2006-Apr-10 17:35 UTC
Re: Re: Prototype Ajax - How to pass my own params to onComplete
Erin Brewer wrote:> Cool, that works nicely. > > I''ve never actually come across the term ''closure'' before in JS.They appear in almost every dynamic language that supports lexical scoping (that I know of). Wikipedia has a nice description: Closure lexical variables differ from global variables in that they do not occupy the global variable namespace. They differ from object oriented object variables in that they are bound to functions, not objects.> Did a > quick search and read a couple things (still don''t quite understand > exactly how that works to get the response reference?), but from my > reading, should i do anything after the clousre to avoid memory leaks? > This same function flow may be invoked multiple times per page depending > on the nature of the event (event1 does ajax stuff, response triggers > event2, etc.)You shouldn''t have to worry about memory leaks (famous last words of JS programmers). That doesn''t mean you won''t and I''ve heard that IE doesn''t handle all cases nicely, but I''m not sure about the specifics. Calling the function that returns the anonymous function multiple times shouldn''t be a problem. Each function will have it''s own lexical pad. Here''s a little write up of the IE memory leak problem. The key is to avoid circular-references. http://jibbering.com/faq/faq_notes/closures.html#clMem -- Michael Peters Developer Plus Three, LP
Martin Bialasinski
2006-Apr-10 19:39 UTC
Re: Prototype Ajax - How to pass my own params to onComplete ?
On 4/10/06, Michael Peters <mpeters-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> change this function to return another function. Something like this:Yes, this is also what .bind() does to form a closure without having to change NamedActionResponseHandler> And then call it like this: > onComplete: NamedActionResponseHandler(theElement),onComplete: NamedActionResponseHandler.bind(this, theElement) theElement will now be passed as an additional argument to NamedActionResponseHandler, when it is called via onComplete. Bye, Martin
Erin Brewer
2006-Apr-10 21:09 UTC
Re: Prototype Ajax - How to pass my own params to onComplete
I like the technique you describe here, but when i try to reference ''theElement'' in NamedActionResponseHandler() it is not defined. I changed the onComplete def to: onComplete: NamedActionResponseHandler.bind(this, theElement) And changed the function to function NamedActionResponseHandler(theResponse, theElement) In the function I get ''theResponse'' as the returned xml still, but ''theElement'' is undefined, am i missing something? Martin Bialasinski wrote:> On 4/10/06, Michael Peters <mpeters-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote: >> change this function to return another function. Something like this: > > Yes, this is also what .bind() does to form a closure without having > to change NamedActionResponseHandler > >> And then call it like this: >> onComplete: NamedActionResponseHandler(theElement), > > onComplete: NamedActionResponseHandler.bind(this, theElement) > > theElement will now be passed as an additional argument to > NamedActionResponseHandler, when it is called via onComplete. > > Bye, > Martin-- Posted via http://www.ruby-forum.com/.
Todd Ross
2006-Apr-10 21:32 UTC
Re: Re: Prototype Ajax - How to pass my own params to onComplete
On 4/10/06, Erin Brewer <erin-A+eRycXGxsFBDgjK7y7TUQ@public.gmane.org> wrote:> I like the technique you describe here, but when i try to reference > ''theElement'' in NamedActionResponseHandler() it is not defined. > > I changed the onComplete def to: > onComplete: NamedActionResponseHandler.bind(this, theElement) > > And changed the function to > function NamedActionResponseHandler(theResponse, theElement) > > In the function I get ''theResponse'' as the returned xml still, but > ''theElement'' is undefined, am i missing something?swap theElement and theResponse in your function NamedActionResponseHandler definition. Your optionally bind()''ed parameters are passed first. Gregory, bind() does support this. It''s my preferred technique. Todd
Erin Brewer
2006-Apr-10 21:50 UTC
Re: Re: Prototype Ajax - How to pass my own params to onComp
Sorry, i should have clarified, i actually had the params for NamedActionResponseHandler specified in the opposite order originally, but the 2nd argument was a undefined object and the first argument when i looked inside it was the response xml from ajax. Seems like my extra parameter was not bound to the call. I''m currently using Prototype 1.3.1, perhaps i need the latest version? Todd Ross wrote:> On 4/10/06, Erin Brewer <erin-A+eRycXGxsFBDgjK7y7TUQ@public.gmane.org> wrote: >> ''theElement'' is undefined, am i missing something? > swap theElement and theResponse in your function > NamedActionResponseHandler definition. Your optionally bind()''ed > parameters are passed first. > > Gregory, bind() does support this. It''s my preferred technique. > > Todd-- Posted via http://www.ruby-forum.com/.
Todd Ross
2006-Apr-10 21:55 UTC
Re: Re: Re: Prototype Ajax - How to pass my own params to onComp
On 4/10/06, Erin Brewer <erin-A+eRycXGxsFBDgjK7y7TUQ@public.gmane.org> wrote:> Sorry, i should have clarified, i actually had the params for > NamedActionResponseHandler specified in the opposite order originally, > but the 2nd argument was a undefined object and the first argument when > i looked inside it was the response xml from ajax. > Seems like my extra parameter was not bound to the call. > I''m currently using Prototype 1.3.1, perhaps i need the latest version?It''s supported as of version 1.4.0; maybe earlier, I don''t know. Do you have a live site I could look at? Are you seeing this behavior in Firefox? I don''t have easy access to Internet Explorer. Todd
Martin Bialasinski
2006-Apr-10 22:02 UTC
Re: Re: Prototype Ajax - How to pass my own params to onComplete
On 4/10/06, Todd Ross <rails-spinoffs-25kFIyuv2iRiLUuM0BA3LQ@public.gmane.org> wrote:> swap theElement and theResponse in your function > NamedActionResponseHandler definition. Your optionally bind()''ed > parameters are passed first.You are right, got that mixed up. Although> ''theResponse'' as the returned xml stillpuzzles me. This could only be if he didn''t use bind(). Anyway, just as a reference that it does work generally: function test(text, number) { alert(text + number) } var fun = test.bind(this, "hello"); fun(1); // alerts hello1 Bye, Martin
Erin Brewer
2006-Apr-10 23:11 UTC
Re: Re: Re: Prototype Ajax - How to pass my own params to on
I updated prototype to 1.4.0 and now your technique works just fine. I''m using IE 6. Todd Ross wrote:>> Seems like my extra parameter was not bound to the call. >> I''m currently using Prototype 1.3.1, perhaps i need the latest version? > > It''s supported as of version 1.4.0; maybe earlier, I don''t know. Do > you have a live site I could look at? > > Are you seeing this behavior in Firefox? I don''t have easy access to > Internet Explorer. > > Todd-- Posted via http://www.ruby-forum.com/.
Seemingly Similar Threads
- RE: Prototype Ajax - How to pass my own params toonComplete ?
- RE: Re: Prototype Ajax - How to pass my own params toonComplete
- function onComplete how to overload?
- PeriodicalUpdater onComplete broken?
- RE: Prototype: correct useage of onComplete withAjax.PeriodicalUpdater