epanepucci-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-18 13:55 UTC
prototype / dom scope
Hi There, First my environment: Firefox 2.0.0.3 on Linux (Extensions: Firebug, Webdeveloper, Aardvark) I have a webpage (XHTML 1.0 Transitional) in which after clicking on a link, I load a little <form> via Ajax.Updater. The form contains buttons which when clicked will call a javascript function defined in the initial webpage. One of these JS functions is: function enable_form_edit(aform) { alert(Prototype.Version); Form( aform ).enable(); } the onclick which calls enable_form_edit() is: onclick="enable_form_edit($(this).up(''FORM''));" The result is that the alert() actually displays the prototype version that I have (1.5.1_rc2) but I get this error on my Firebug console:>> Form is not a function >> Form(aform).enable();which is pointing to the JS function above. on the other hand, if in the enabe_form_edit() I have aform.enable(); then this works fine. Questions: - why can''t I use "Form" as a singleton? - why is Prototype.Version available but not Form? Thanks much! Zac --~--~---------~--~----~------------~-------~--~----~ 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 Apr 18, 11:55 pm, "epanepu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <epanepu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi There, > > First my environment: > Firefox 2.0.0.3 on Linux (Extensions: Firebug, Webdeveloper, > Aardvark) > > I have a webpage (XHTML 1.0 Transitional) in which after clicking on a > link, > I load a little <form> via Ajax.Updater. > > The form contains buttons which when clicked will > call a javascript function defined in the initial webpage. > > One of these JS functions is: > > function enable_form_edit(aform) { > > alert(Prototype.Version); > Form( aform ).enable(); > > } > > the onclick which calls enable_form_edit() is: > > onclick="enable_form_edit($(this).up(''FORM''));"Consider: onclick="enable_form_edit(this.form);" but if all the function does is enable the form, why not: onclick="$(this.form).enable();"> The result is that the alert() actually displays the prototype version > that I have (1.5.1_rc2) but I get this error on my Firebug console: > > >> Form is not a function > >> Form(aform).enable(); > > which is pointing to the JS function above.Because Form isn''t a function, it''s a plain object with some properties. Look at line 2548: var Form = { ... } One of the properties added to the Form object is called ''enable'', it is a function so you can use: Form.enable() In Firefox and other browsers that allow the basic document elements to be extended, all form objects in the document are extended with the same properties as the Prototype Form object so in Firefox you can do: <form> <input type="button" onclick="alert(typeof this.form.enable);" value="click"> </form> and you will see an alert with "function", but in IE you''ll see ''undefined''. So to be cross-browser you must do: onclick="alert(typeof $(this.form).enable);"> > on the other hand, if in the enabe_form_edit() I have > aform.enable(); > > then this works fine.Because the enable property is a reference to a function object, so you can use the call operator with it.> Questions: > - why can''t I use "Form" as a singleton?Form is an object, it is not a function, using the call operator () with something other than a function will create a runtime error.> - why is Prototype.Version available but not Form?Prototype is declared as a global object, it is assigned a property called Version (among other things). When Prototype.Version is evaluated, it returns whatever the value of Version has been set to. The first couple of lines of Prototype.js: var Prototype = { Version: ''1.5.1_rc2'', ... } so any time you like after the script element that loads Prototype.js you can do alert(Prototype.Version) and you will see ''1.5.1_rc2'' (provided javascript is available and enabled). -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
epanepucci-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-19 07:49 UTC
Re: prototype / dom scope
> Because Form isn''t a function, it''s a plain object with some > properties. Look at line 2548: > > var Form = { > ... > }I had seen that, but *Element* is declared just like *Form* and Element(''blabla'').hide() works. This looks more like a bug, *all* of these extensions to the DOM are usually available as Singletons, and besides http://www.prototypejs.org/api/form/reset gives an example using Form as a singleton.> but if all the function does is enable the form, why not: > > onclick="$(this.form).enable();"It is not all it does, I was just being succint. Anyways, thanks for the alternative workarounds. Cheers, Zac --~--~---------~--~----~------------~-------~--~----~ 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 Apr 19, 5:49 pm, "epanepu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <epanepu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Because Form isn''t a function, it''s a plain object with some > > properties. Look at line 2548: > > > var Form = { > > ... > > } > > I had seen that, but *Element* is declared just like *Form* and > Element(''blabla'').hide() > works.It does? I get "Element is not a function", just like I get with Form.> > This looks more like a bug, *all* of these extensions to the DOM are > usually > available as Singletons,Maybe that''s causing confusion. They aren''t "extensions", they are objects with properties. Some (most?) of those properties are references to function objects. For Firefox et al, the base DOM objects are extended with the same function objects. For IE and similar, they aren''t. Don''t think of javascript as a classic object oriented language - it isn''t, it''s *object based*.> and besideshttp://www.prototypejs.org/api/form/reset > gives an example using Form as a singleton.The example is: Form.reset(''contact'') which is using the reset property of the Form object which expects either an ID or a form object as the parameter. It is not attempting to "call" the Form object like Form() which will produce a runtime error. Try: alert( typeof Form ) // shows object alert( typeof Form.reset ) // shows function alert( typeof Element ) // shows object alert( typeof Element.toggle ) // shows function It would probably have been better if Prototype hadn''t named it''s own objects the same as other DOM and javascript objects. e.g. calling the library ''Prototype.js'' causes plenty of confusion with those trying to learn about the javascript prototype property and object which are totally different to the Prototype.js library and the Prototype object declared within it.> > > but if all the function does is enable the form, why not: > > > onclick="$(this.form).enable();" > > It is not all it does, I was just being succint.Fine. :-) -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
epanepucci-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-19 11:14 UTC
Re: prototype / dom scope
>The example is: > > Form.reset(''contact'')ahhhh! thank''s for clearly pointing that out... I feel like an idiot now, but better knowing than not. Thanks again for wasting your time with me. Cheers, Zac --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---