Yes, all this is do-able. The only part that seems unusual to me is
this:
document.observe('custom:event',
onCustomEvent.bindAsEventListener(event.memo.firingWidget));
If you want the scope correction to happen "automatically," this won't
do it -- the "event" variable you're trying to read from doesn't exist
except within the scope of the handling function.
If you want to extract the general pattern of firing an event handler
in the scope of a custom property set on the event, you can do
something like this:
Function.customEventScope = function(fn) {
return function() {
return fn.apply(arguments[0].memo.firingWidget, arguments);
};
};
Then, in your class...
document.observe('custom:event',
Function.customEventScope(onCustomEvent));
Now, this is all do-able, but I wonder if it's necessary, since you
say you've got a solution already working. If you were building it
from scratch, this is a great way to get extra mileage out of the
custom event system. But it is somewhat awkward nonetheless.
Cheers,
Andrew
On Feb 29, 8:33 am, Keith Hughitt <[EMAIL PROTECTED]> wrote:
> Do you think that prototype's custom event handling mechanism is
> suitable for handling an
> entire event-driven application?
>
> I have a large number of events which are fired by class methods.
> Currently I have
> some auxillary functions which all classes inherit that enable them to
> subscribe to
> and notify others about important events.
>
> I considered porting this functionality to make use of prototype 1.6's
> custom event functionality,
> but I have run into some barriers and am wondering if it is even a
> good idea.
>
> Basically I want to have some methods fire events, and pass a
> reference to the parent class along.
>
> document.fire('custom:event', {firingWidget:widget});
>
> An observer would then catch the custom event and respond, treating
> the event as if it
> were fired by the referenced class, and not by document, something
> like:
>
> document.observe('custom:event',
> onCustomEvent.bindAsEventListener(event.memo.firingWidget));
>
> Does this look do-able? Furthermore, would it make sense to switch to
> using prototype's custom events?
> The system works fine at the moment using it's own observer-like event
> system, however, if prototype
> can fully support custom events now, it doesn't seem like it is
> necessary to have a superclass just for event-handling
> functionality.
>
> Any advice or thoughts on the subject would be very much
> appreciated. :)
>
> Thanks!
> Keith
>
> On Jan 20, 5:51 am, Christophe Porteneuve <[EMAIL PROTECTED]> wrote:
>
> > 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 acustomevent. 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'customevent 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
-~----------~----~----~----~------~----~------~--~---