Re: from 1.6.0.1 to 1.7.1: alternative to Event.cache and ._eventID?

59 views
Skip to first unread message

Victor

unread,
Aug 16, 2012, 6:05:17 AM8/16/12
to prototype-s...@googlegroups.com
In prototype 1.6.0.1 I used to use Event.cache[formElement._eventID] to check if an element has attached events.

Why do you need such check at all? If you need to remove all event observers from element - there is #stopObserving(), if you need to remove from element and its children - there is #purge().

Look at https://github.com/victor-homyakov/VisualEvent/blob/master/js/parsers/prototype1.7.js - this code enumerates attached event handlers.

dipnlik

unread,
Aug 16, 2012, 9:55:24 AM8/16/12
to prototype-s...@googlegroups.com
I use this check in a jasmine spec to ensure a newly created element
has attached events. The element is supposed to have events attached
to keydown and blur, and altough right now I'm not exactly testing
that, at least I could test that the element had attached events. If
there are better ways to test that, I'd love to know.

Will take a look at VisualEvents soon, maybe that'll be useful in my
test environment. Thanks!

--
:: dip
--
> --
> You received this message because you are subscribed to the Google Groups
> "Prototype & script.aculo.us" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/prototype-scriptaculous/-/tBFOZrZJ_gAJ.
>
> To post to this group, send email to
> prototype-s...@googlegroups.com.
> To unsubscribe from this group, send email to
> prototype-scripta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/prototype-scriptaculous?hl=en.

Victor

unread,
Aug 20, 2012, 6:15:46 AM8/20/12
to prototype-s...@googlegroups.com
I use this check in a jasmine spec to ensure a newly created element
has attached events.  The element is supposed to have events attached
to keydown and blur

How exactly events are attached? With element.observe() or element.on()? Directly to element or to one of its ancestors (with event delegation)? If event observer is attached with element.on() then there is no possibility to distinct one observer from another - we can check that some observer is attached to desired event, but cannot prove that this is our observer. It event delegation is used then things are much worse - we can check just that some observer is attached to e.g. blur event on ancestor of element under test.

I could simplify event cache parser to this code:

for (var e in Event.cache) {
  var event = Event.cache[e];
  if (event.element === YOUR_ELEMENT) {
    var handlers = event.blur;
    if (handlers.length > 0) {
      // possible match - at least someone is observing blur event
    }
    for (var i = 0, len = handlers.length; i < len; i++) {
      if (handlers[i].handler === YOUR_OBSERVER) {
        // perfect match, does not work with element.on()
      }
    }
  }
}

Reply all
Reply to author
Forward
0 new messages