Hi all, I have been having some trouble understanding Event.observe() issues, and was hoping someone might be able to help me out. Why is it that I need to use curry() whenever I have a callback function with > 0 arguments? For example, If I have an element with id=''test'' and do: var bla = function(msg) {console.log(msg);} document.observe(''dom:loaded'', function() { $(''test'').observe(''click'', bla.curry(''my msg'')); }); It works fine. If I remove the curry, however, and just try "$ (''test'').observe(''click'', bla(''my msg''));" bla will be called once when the document is loaded, and then refuse to work afterwards. Furthermore, if I have a function bla() which *does not* include any arguments, it works fine without the curry. Can anyone explain to me what it going on here? I tried looking around the archives, and found some related posts, but they did not seem to address the same question. Any help would be greatly appreciated. Thanks, Keith --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
.curry() returns a function reference, which is what .observe expects. Your second example is not a function reference, but rather a function execution. The function executes right away inline when the .observe() is initially hit, and since it does not return some different function reference, there is no function reference available to the observe method to execute later. What''s really supposed to be there is a function reference, so it can be executed at a later time (i.e. when the event happens). so .observe("eventName", bla) works, while .observe("eventName", bla()) does not another way, which you may see in practice, is to define an anonymous function that calls out to your function: .observe("eventName", function() { bla("something"); }); the anonymous function definition (closure) is the same thing as a function reference, just not applied to a variable beforehand. I hope this very general explanation helps. Another alternative to .curry() which you''ll see in practice is .bind() or .bindAsEventListener() which both act upon a function and return a wrapped function reference. On 2/28/08, Keith Hughitt <keith.hughitt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hi all, > > I have been having some trouble understanding Event.observe() issues, > and was hoping someone might be able to help me out. > > Why is it that I need to use curry() whenever I have a callback > function with > 0 arguments? For example, > If I have an element with id=''test'' and do: > > var bla = function(msg) {console.log(msg);} > > document.observe(''dom:loaded'', function() { > $(''test'').observe(''click'', bla.curry(''my msg'')); > }); > > It works fine. > > If I remove the curry, however, and just try "$ > (''test'').observe(''click'', bla(''my msg''));" bla will be called once > when the document is loaded, and then refuse to work afterwards. > Furthermore, if I have a function bla() which > *does not* include any arguments, it works fine without the curry. > > Can anyone explain to me what it going on here? I tried looking around > the archives, and found some > related posts, but they did not seem to address the same question. > > Any help would be greatly appreciated. > > > Thanks, > Keith > > > > >-- 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 -~----------~----~----~----~------~----~------~--~---
Thanks Ryan, That answered my question entirely. I didn''t realize that Event#observe expects a reference to a function, but now that I know that things are much clearer. I appreciate the clarification! Take care, Keith On Feb 28, 3:00 pm, "Ryan Gahl" <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> .curry() returns a function reference, which is what .observe expects. Your > second example is not a function reference, but rather a function execution. > The function executes right away inline when the .observe() is initially > hit, and since it does not return some different function reference, there > is no function reference available to the observe method to execute later. > What''s really supposed to be there is a function reference, so it can be > executed at a later time (i.e. when the event happens). > > so .observe("eventName", bla) works, while .observe("eventName", bla()) does > not > > another way, which you may see in practice, is to define an anonymous > function that calls out to your function: > > .observe("eventName", function() { bla("something"); }); > > the anonymous function definition (closure) is the same thing as a function > reference, just not applied to a variable beforehand. > > I hope this very general explanation helps. > > Another alternative to .curry() which you''ll see in practice is .bind() or > .bindAsEventListener() which both act upon a function and return a wrapped > function reference. > > On 2/28/08, Keith Hughitt <keith.hugh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi all, > > > I have been having some trouble understanding Event.observe() issues, > > and was hoping someone might be able to help me out. > > > Why is it that I need to use curry() whenever I have a callback > > function with > 0 arguments? For example, > > If I have an element with id=''test'' and do: > > > var bla = function(msg) {console.log(msg);} > > > document.observe(''dom:loaded'', function() { > > $(''test'').observe(''click'', bla.curry(''my msg'')); > > }); > > > It works fine. > > > If I remove the curry, however, and just try "$ > > (''test'').observe(''click'', bla(''my msg''));" bla will be called once > > when the document is loaded, and then refuse to work afterwards. > > Furthermore, if I have a function bla() which > > *does not* include any arguments, it works fine without the curry. > > > Can anyone explain to me what it going on here? I tried looking around > > the archives, and found some > > related posts, but they did not seem to address the same question. > > > Any help would be greatly appreciated. > > > Thanks, > > Keith > > -- > Ryan Gahl > Manager, Senior Software Engineer > Nth Penguin, LLChttp://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 -~----------~----~----~----~------~----~------~--~---