How can I simulate stopImmediatePropagation () behavior (without using jquery)

I am working on event handling code for the Javascript library, and I am trying to implement something similar to stopImmediatePropagation (), which will also work in IE 6.

The way to handle events at present is that our event transmission code is registered with the object, and then users register all their events using our event handlers.

The first way I tried to emulate stopImmediatePropagation () was to simply add this method to the event if it does not already exist:

if (event != null && event.isImmediatePropagationEnabled == null) { event.stopImmediatePropagation = function () { this.isImmediatePropagationEnabled = false; }; event.isImmediatePropagationEnabled = true; event.isImmediatePropagationStopped = function () { return !this.isImmediatePropagationEnabled; }; } 

When user event handler callbacks are called, they are passed to the same event object that we receive. They can call stopImmediatePropagation () on the event if they wish.

When we focus on all the event handlers registered with us, we check the distribution of boolean each time:

 given some event for [all the callbacks] { if (event != null && event.isImmediatePropagationStopped != null && event.isImmediatePropagationStopped()) { stopPropagation = true; break; } execute the callback, passing in the event } 

This works fine in some browsers. Since the event is saved even when our event-handling code completes and the event bubbles up to the next element, after our event-transfer code hits again, the isImmediatePropagationStopped property still exists, and therefore the callbacks (which are registered with us) are no longer executed.

In Internet Explorer (even in 8) this does not work. Everything is fine on the same element; but after the appearance of the event bubbles, an entirely new event object is created, and we lose the isImmediatePropagationStopped property. This means that we cannot check whether the user has disabled distribution.

So my question is: does anyone have any ideas on how to do this? I know that jquery manages a similar feat ( http://bugs.jquery.com/ticket/3355 ), but they do it the same way - storing it in additional data for the object. I do not know how the instability of an object does them harm, just as I do. (And for various reasons, using jquery itself is not an option here)

If anyone has any ideas - either because I know more about Javascript than I do, or because they might come up with a neat way to do this, I would really appreciate it. Thanks!

+6
source share
1 answer

Try changing your function a bit:

 event.stopImmediatePropagation = function () { this.isImmediatePropagationEnabled = false; this.cancelBubble = true; }; 

This โ€œcancelBubbleโ€ flag is an IE thing, and it should prevent this event from activating the parent chain of DOM elements.

+6
source

Source: https://habr.com/ru/post/891163/


All Articles