brennanmusic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-08  23:10 UTC
Event.observe: problem dynamically loading handler functions
I''m writing a script to generate a control panel form an XML file and
I''m having trouble when attempting to attach events using
Event.observer.
An example node in my XML file looks like this:
<control ... >
<function action="click" do="doSomething" />
</control>
where "click" is the event and "doSomething" is the name of
a
javascript function I have defined elsewhere.
My javascript looks something like this (the control nodes have already
been appended to the DOM as <a href..>''s):
var controls = $A(XML.getElementsByTagName("control"));
controls.each(function(node) {
			var nFunctions = $A(node.getElementsByTagName("function"));
			nFunctions.each(function(fun){
Event.observe(node.getAttribute("id"),fun.getAttribute("action"),fun.getAttribute("do"))
			});
		});
I know the problem is with the last argument in Event.observe because
if I change the last argument to an anonymous function it works. It
also works if I change the last argument so that it is the name of the
function. These both work:
Anonymous function:
Event.observe(node.getAttribute("id"),fun.getAttribute("action"),function(e){alert(e);})
Name of function:
Event.observe(node.getAttribute("id"),fun.getAttribute("action"),doSomething)
This is the error I''m getting:
[Exception... "Could not convert JavaScript argument" nsresult:
"0x80570009
(NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame::
http://.../prototype.js :: anonymous :: line 1969" data: no]
Any help greatly appreciated. Thanks!
.
James
--~--~---------~--~----~------------~-------~--~----~
 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
-~----------~----~----~----~------~----~------~--~---
James
2006-Nov-09  00:28 UTC
Re: Event.observe: problem dynamically loading handler functions
err... so, it works using eval
eval("Event.observe(node.getAttribute(''id''),fun.getAttribute(''action''),"+fun.getAttribute("do")+")");
I''m going to run with this for now, but I like to avoid eval so if
anyone has a better idea get at me.
Thanks,
James
--~--~---------~--~----~------------~-------~--~----~
 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
-~----------~----~----~----~------~----~------~--~---
Ryan Gahl
2006-Nov-09  02:05 UTC
Re: Event.observe: problem dynamically loading handler functions
What you''re doing in the first example is essentially just passing a
string
in as the 3rd param, where it''s expecting a function pointer. It works
in
your eval because the whole statement is constructed as a string and then
interpreted.
If you are working in a object oriented context... where "myObj" is
some
object which has a function named "doSomething", the following will
work, as
it will return a pointer to the myObj.doSomething function... if you are
working with a only globally scoped functions, try replacing "myObj"
with
"window" below...
Event.observe(node.getAttribute(''id''),
fun.getAttribute(''action''), myObj[
fun.getAttribute(''do'')] );
On 11/8/06, James <brennanmusic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
>
> err... so, it works using eval
>
>
eval("Event.observe(node.getAttribute(''id''),fun.getAttribute
>
(''action''),"+fun.getAttribute("do")+")");
>
> I''m going to run with this for now, but I like to avoid eval so if
> anyone has a better idea get at me.
>
> Thanks,
> James
>
>
> >
>
-- 
Ryan Gahl
Application Development Consultant
Athena Group, Inc.
Inquire: 1-920-955-1457
Blog: http://www.someElement.com
--~--~---------~--~----~------------~-------~--~----~
 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
-~----------~----~----~----~------~----~------~--~---
James
2006-Nov-09  04:52 UTC
Re: Event.observe: problem dynamically loading handler functions
Thanks Ryan, that did the trick! . James --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---