Event.stopObserving requires the exact same arguments that were passed
to Event.observe in order to stop the event. This can be tricky
sometimes, as the example in the doc using obj.fx.bindAsEventListener
returns a new function, breaking the match. The doc recommends caching
the function in a variable. This could be simplified.

Event.observe should return an array of the arguments passed. You
could then pass the array as the only argument to Event.stopObserving
and it would stop observing the event as expected. This code is
backwards-compatible with the current way. The modified code for this
is very simple:

  observe: function(element, name, observer, useCapture) {
    element = $(element);
    useCapture = useCapture || false;

    if (name == 'keypress' &&
      (Prototype.Browser.WebKit || element.attachEvent))
      name = 'keydown';

    Event._observeAndCache(element, name, observer, useCapture);
+    return [element, name, observer, useCapture];
  },

  stopObserving: function(element, name, observer, useCapture) {
+    if(element instanceof Array) {
+      name = element[1];
+      observer = element[2];
+      useCapture = element[3];
+      element = element[0];
+    }
    element = $(element);
    useCapture = useCapture || false;

    if (name == 'keypress' &&
        (Prototype.Browser.WebKit || element.attachEvent))
      name = 'keydown';

    if (element.removeEventListener) {
      element.removeEventListener(name, observer, useCapture);
    } else if (element.detachEvent) {
      try {
        element.detachEvent('on' + name, observer);
      } catch (e) {}
    }
  }

With this code, you could do something like this:

var evt = Event.observe(window, "click", function() {
  alert("test");
});

And stop it like this:

Event.stopObserving(evt);

Is this good?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to