GarethAtFlignet a écrit :
> Event.observe("class:method", function(event) {
>     // do something
> });

Yes, except you need to observe on a DOM node, so you'd insert, say, 
document as a first argument.

> Now I understand that you can fire 'custom' events:
> window.fire("class:method", { blah: 0 });

Almost.  "fire" exists only on DOM elements and the document node, so 
"document" instead of "window."

The trick here is to take an existing method and turn it into the same 
method that also fires a custom event.  That's AOP, and in Prototype you 
can achieve that with the wrap method.  Consider this:

YourClass.prototype.yourMethod = YourClass.prototype.yourMethod.wrap(
   function(proceed, arg1, arg2) {
     document.fire('your:event');
     proceed(arg1, arg2);
   }
);

Then whenever one of your instances of YourClass gets its yourMethod 
called (assuming here it takes two arguments, but customize for your 
case), it'll delegate tot he original implementation and fire the 
'your:event' custom event on the document node.  To trigger the event 
before the original behavior, just switch lines in the new function.

You can also pass extra data as an additional object in 2nd argument to 
document.fire, which will be accessible through the event's memo property.

To listen for the event:

document.observe('your:event', function(event) { ... });

'HTH

-- 
Christophe Porteneuve aka TDD
[EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to