Is there a way to invoke an anonymous function and have the function itself returned? What I am trying to do is attach an observer to an element, and at the same time run the observer so that the initial state is set. My javascript looks like this: function toggleDetails () { $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : ''show'' ); } $(''showDetails'').observe( ''change'', toggleDetails ); toggleDetails(); Bascially, it adds a control to show the "moreDetails" sections of a form when a toggle is set to "yes". But I feel like this could be done in less code... maybe using an anonymous function that is invoked as it is attached as an observer. Something like this: $(''showDetails'').observe( ''change'', function () { $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : ''show'' ); }.invokeAndReturn() ); Where invokeAndReturn() is a method that calls "this", discards the results, and then just returns "this". Am I over complicating this? Thanks for any help you can offer --~--~---------~--~----~------------~-------~--~----~ 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 would either go the plain way: document.body.observe(''click'', (function() { myFunc(); return myFunc; })() ) or define a Function.prototype.* helper: Function.prototype.returnInvoke = function() { this(); return this; } document.body.observe(''click'', myFunc.returnInvoke() ) Best, kangax --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
it''s easiest to just assign it to local variable: var f = function () { $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : ''show'' ); }; f(); $(''showDetails'').observe( ''change'', f ); if you''re running that script within another function it won''t be leaked to the global scope so you should be all set. I''d expect you''re probably doing something along these lines: $(window).observe(''load'', function(){ var f = function () { $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : ''show'' ); }; f(); $(''showDetails'').observe( ''change'', f ); }); On Feb 6, 7:53 pm, Nycto <NyctoFi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there a way to invoke an anonymous function and have the function > itself returned? > > What I am trying to do is attach an observer to an element, and at the > same time run the observer so that the initial state is set. My > javascript looks like this: > > function toggleDetails () { > $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : > ''show'' );} > > $(''showDetails'').observe( ''change'', toggleDetails ); > toggleDetails(); > > Bascially, it adds a control to show the "moreDetails" sections of a > form when a toggle is set to "yes". > > But I feel like this could be done in less code... maybe using an > anonymous function that is invoked as it is attached as an observer. > Something like this: > > $(''showDetails'').observe( ''change'', function () { > $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : > ''show'' ); > > }.invokeAndReturn() ); > > Where invokeAndReturn() is a method that calls "this", discards the > results, and then just returns "this". > > Am I over complicating this? > > Th anks for any help you can offer--~--~---------~--~----~------------~-------~--~----~ 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 Feb 6, 2008 10:53 PM, Nycto <NyctoFixer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Is there a way to invoke an anonymous function and have the function > itself returned? > > What I am trying to do is attach an observer to an element, and at the > same time run the observer so that the initial state is set. My > javascript looks like this: > > function toggleDetails () { > $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : > ''show'' ); > } > $(''showDetails'').observe( ''change'', toggleDetails ); > toggleDetails();function toggleDetails() { // your code return arguments.callee; } $("showDetails").observe("change", toggleDetails()); That should work -Nicolas> Bascially, it adds a control to show the "moreDetails" sections of a > form when a toggle is set to "yes". > > But I feel like this could be done in less code... maybe using an > anonymous function that is invoked as it is attached as an observer. > Something like this: > > $(''showDetails'').observe( ''change'', function () { > $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : > ''show'' ); > }.invokeAndReturn() ); > > Where invokeAndReturn() is a method that calls "this", discards the > results, and then just returns "this". > > Am I over complicating this? > > Thanks for any help you can offer > > >--~--~---------~--~----~------------~-------~--~----~ 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 for the help, guys. I decided to add a helper function: // Invokes a function, discards the result, and returns the function itself Function.prototype.inlineCall = function () { if (arguments.length < 1) this(); else this.apply( $A(arguments).shift(), $A(arguments).slice(1)); return this; } On Feb 7, 6:05 am, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 6, 2008 10:53 PM, Nycto <NyctoFi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Is there a way to invoke an anonymous function and have the function > > itself returned? > > > What I am trying to do is attach an observer to an element, and at the > > same time run the observer so that the initial state is set. My > > javascript looks like this: > > > function toggleDetails () { > > $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : > > ''show'' ); > > } > > $(''showDetails'').observe( ''change'', toggleDetails ); > > toggleDetails(); > > function toggleDetails() { > // your code > return arguments.callee; > > } > > $("showDetails").observe("change", toggleDetails()); > > That should work > -Nicolas > > > Bascially, it adds a control to show the "moreDetails" sections of a > > form when a toggle is set to "yes". > > > But I feel like this could be done in less code... maybe using an > > anonymous function that is invoked as it is attached as an observer. > > Something like this: > > > $(''showDetails'').observe( ''change'', function () { > > $$(''.moreDetails'').invoke( $F(''showDetails'') == ''y'' ? ''hide'' : > > ''show'' ); > > }.invokeAndReturn() ); > > > Where invokeAndReturn() is a method that calls "this", discards the > > results, and then just returns "this". > > > Am I over complicating this? > > > Thanks for any help you can offer--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---