Hello. I have a function that I''m calling several times, and different ID''s are passed in. How do I know which particular ID is being written back to? Here''s the function in question: function updateCount(sId) { new Ajax.Updater(sId, ''http://url'''', { onComplete: function(r) { if (r.responseText == ''-1'') { location.reload(true); } else if (isNaN(parseInt(r.responseText))) { // We need to write "0" to the correct DIV! alert(r.responseText); } } }); } I have a check in place to determine if the returned result is numeric, but I need to update the appropriate DIV. The param "sId" contains the element to be written to, and I need to know where in prototype this is stored so that I can reference that ID and take the appropriate action. Thanks in advance. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
semi-sentient wrote:> I have a check in place to determine if the returned result is > numeric, but I need to update the appropriate DIV. The param "sId" > contains the element to be written to, and I need to know where in > prototype this is stored so that I can reference that ID and take the > appropriate action.Just use the variable. It''s not stored anywhere in the object or it''s prototype, it''s just automatically available to it''s lexical scope. It''s called a "closure" (http://en.wikipedia.org/wiki/Closure_%28computer_science%29). -- 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 -~----------~----~----~----~------~----~------~--~---
Michael Peters wrote:> Just use the variable. It''s not stored anywhere in the object or it''s prototype, > it''s just automatically available to it''s lexical scope. It''s called a "closure" > (http://en.wikipedia.org/wiki/Closure_%28computer_science%29).I should clarify that for anyone else who''s as pedantic as I am :) Javascript doesn''t have real lexical scope and just has subroutine scope. So the closure does exist but you can get unexpected (well unexpected if you''re used to real lexical scop) results if you try using a closure in a loop with the looping variables. -- 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 -~----------~----~----~----~------~----~------~--~---
I''m not sure I understand. How can I use that variable and be assured that (assuming you mean "sId" parameter) it writes back to the correct DIV? For example, on one particular page I call that function 3 times in sequence: updateCount(''div_one''); updateCount(''div_two''); updateCount(''div_three''); Each time the function is called it''s with a different ID, so when "onComplete" fires, I need to know which particular ID is going to be written to so that I can write "n/a" in the event that a numeric string was not returned. If the 2nd call fails, for example, then how do I know to write "n/a" to "div_two" and not "div_three"? I hope that makes sense. - Markus On Nov 20, 9:49 am, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> Michael Peters wrote: > > Just use the variable. It''s not stored anywhere in the object or it''s prototype, > > it''s just automatically available to it''s lexical scope. It''s called a "closure" > > (http://en.wikipedia.org/wiki/Closure_%28computer_science%29). > > I should clarify that for anyone else who''s as pedantic as I am :) > Javascript doesn''t have real lexical scope and just has subroutine scope. So the > closure does exist but you can get unexpected (well unexpected if you''re used to > real lexical scop) results if you try using a closure in a loop with the looping > variables. > > -- > 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 -~----------~----~----~----~------~----~------~--~---
He means doing new Ajax.Updater(sld, { ..., onComplete: function() { if (sld == "something) { ... } } }); Best, -Nicolas On Nov 20, 2007 1:46 PM, Michael Peters <mpeters-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> > semi-sentient wrote: > > > I have a check in place to determine if the returned result is > > numeric, but I need to update the appropriate DIV. The param "sId" > > contains the element to be written to, and I need to know where in > > prototype this is stored so that I can reference that ID and take the > > appropriate action. > > Just use the variable. It''s not stored anywhere in the object or it''s > prototype, > it''s just automatically available to it''s lexical scope. It''s called a > "closure" > (http://en.wikipedia.org/wiki/Closure_%28computer_science%29). > > -- > 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 -~----------~----~----~----~------~----~------~--~---
I see. I didn''t think "sId" would be persist for each particular call -- and I guess I should have tested for that first. =P Prototype is cool. =) Thanks again... - Markus On Nov 20, 10:05 am, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> He means doing > > new Ajax.Updater(sld, { > ..., > onComplete: function() { > if (sld == "something) { ... } > } > > }); > > Best, > -Nicolas > > On Nov 20, 2007 1:46 PM, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote: > > > > > semi-sentient wrote: > > > > I have a check in place to determine if the returned result is > > > numeric, but I need to update the appropriate DIV. The param "sId" > > > contains the element to be written to, and I need to know where in > > > prototype this is stored so that I can reference that ID and take the > > > appropriate action. > > > Just use the variable. It''s not stored anywhere in the object or it''s > > prototype, > > it''s just automatically available to it''s lexical scope. It''s called a > > "closure" > > (http://en.wikipedia.org/wiki/Closure_%28computer_science%29). > > > -- > > 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 -~----------~----~----~----~------~----~------~--~---
semi-sentient wrote:> I see. I didn''t think "sId" would be persist for each particular call > -- and I guess I should have tested for that first. =P > > Prototype is cool. =)I agree, but this isn''t a Prototype feature. It''s part of the JavaScript language. In fact most dynamic languages have closures (Perl, Python, PHP, Ruby) but other "noun" based languages usually don''t (Java, C#) -- 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 -~----------~----~----~----~------~----~------~--~---