Colin Mollenhour wrote:
> This is one of those things that gets to be very tedious when working 
> with Event.observe.
> Currently, you have to store the function like so:
>
> this.onMousedownListener = this.onMousedown.bindAsEventListener(this);
> Event.observe(document, "mousedown", this.onMousedownListener);
> Event.stopObserving(document,"mousedown",this.onMousedownListener);
>   
I understand that cross-browser element observation is very complex when 
you start thinking about caching, stopping observation, and dealing with 
IE's limitations--just look at the comments and links here: 
http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html.

A different approach occurred to me.  What are the thoughts on having 
Event.observe return something?  For example, the code above could become:

this.onMousedownListener = Event.observe(document, "mousedown", 
this.onMousedown.bindAsEventListener(this));
Event.stopObserving(document,"mousedown",this.onMousedownListener);


Or what about an even simpler approach:

var handle = Event.observe(document, 'mousedown', 
this.onMousedown.bindAsEventListener(this));
handle.stopObserving();

...where Event.observe returns a object "handle":

Event.observe = function(element, name, observer) {
  element = $(element);
  ...IE model...
  element.addEventListener(name, observer, false);
  return {
    stopObserving: function() {
      element.removeEventListener(name, observer, false);
    },
    element: element,
    event: name,
    function: observer
  };   
};

The above function would lend itself nicely to the caching of handles, 
stopping all observation of a particular element or element+event, 
clearing all on IE unload, and it could be made to be 100% backwards 
compatible with the use of Event.stopObserving.


What do you think?

-- Ken Snyder

--~--~---------~--~----~------------~-------~--~----~
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