On Aug 24, 4:15 am, arobinson
<Andrew.RW.Robin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I am able to add new methods to the Event object and Event.Methods
> object if necessary using Object.extend(Event, {methods...});, but
> they do not appear on the event object that the browser fires.
>
> Example (loaded after prototype.js):
> Object.extend(Event.Methods, { function testMethod (evt) {} });
Error, Event.Methods is not defined.
The second argument that you are trying to pass to Object.extend would
be interpreted as a syntactically erroneous object literal if the
script got that far. Removing the braces makes it a function
expression, where the name is irrelevant and can be safely removed.
If Event.Methods did exist, you''d add all the properties of the
anonymous function to it, including those on its prototype chain
(which is not a very good idea).
> Object.extend(Event, Event.Methods);
Event.Methods is not defined. What are you actually trying to do? If
you want to add properties to the UA''s Event object, then:
Object.extend(Event, {testMethod: function (evt) {
alert(''hi'');} });
however what I think you are trying to do is extend Event.prototype:
Object.extend(Event.prototype, {testMethod: function (evt)
{ alert(''hi'');}});
which is the same as:
Event.prototype.testMethod = function (evt) {alert(''hi'');} ;
> <input type="text" onkeypress="event.testMethod();"
/>
>
> The above code will fail in firefox as event doesn''t have the
> testMethod.
The result of the assignment is effectively:
<... onkeypress="function(evt){...}">
using the Event object as a namespace, you could have picked any pre-
existing object. You might as well use your own object, rather than
risking overwriting some existing property of Event.
> How can I get this to work well without changing the prototype.js code?
What I think you are trying to do is attach a function that is called
via the event object''s prototype chain, such as:
<script type="text/javascript">
Event.prototype.testMethod = function () {
var el = this.target || this.srcElement;
alert( el.nodeName + '': '' + (el.id? el.id:''no
id'') );
}
function foo(e){
e.testMethod();
}
</script>
<input id="button00" type="button" value="click
me"
onclick="foo(event);">
--
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
-~----------~----~----~----~------~----~------~--~---