hlship-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-27 00:06 UTC
Adding methods to all forms
Something isn''t quite gelling for me. I want to add a few methods to any and all forms in my application. For example: Form.Methods.describe = function(form) { form = $(form); window.alert("I contain: " + this.serialize(form)); }; One I do this, I can invoke Form.Methods.describe(''myform'') but I can''t do: $(''myform'').describe() The latter gets a "describe is not a function" error. How can I hook things so that the latter example works? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The proper way to do this is via Element.addMethods: http://prototypejs.org/api/element/addMethods Colin hlship-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Something isn''t quite gelling for me. > > I want to add a few methods to any and all forms in my application. > > For example: > > Form.Methods.describe = function(form) { > form = $(form); > window.alert("I contain: " + this.serialize(form)); > }; > > One I do this, I can invoke > > Form.Methods.describe(''myform'') > > but I can''t do: > > $(''myform'').describe() > > The latter gets a "describe is not a function" error. > > How can I hook things so that the latter example works? > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 just noticed the docs aren''t complete.. If you want to add the methods for a specific tagName, the first argument is the tag name and the second is the hash of methods. E.g.: Element.addMethods(''form'',{describe: function(){}}); Colin --~--~---------~--~----~------------~-------~--~----~ 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 just noticed the docs aren''t complete.They are... for version 1.5. The feature you are talking about was introduced in v 1.5.1_pre0. So it''s only available to "edge" Prototype. In the meantime, the trick is just to do: Form.Methods.describe = function(form) { form = $(form); window.alert("I contain: " + this.serialize(form)); }; Element.addMethods(); // yes, without ANY argument! As advised in the docs (see bottom of the page). Hope this helps, Tobie --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ahh! I have too many versions of Prototype floating around... hehe tobie wrote:> They are... for version 1.5. >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---