I am writing an event dispatcher in JavaScript and I've decided to base on standard js CustomEvent class. I can't find out how to detect if event propagation was stopped in the listener (via e.stopPropagation()). Should I rather write an Event object implementation myself?
The first solution
The Event.cancelBubble
property is a historical alias to Event.stopPropagation()
. But it has the difference to the function Event.stopPropagation()
because Event.cancelBubble
we could read like follows:
var isPropagationStopped = event.cancelBubble; // true if it was stopped, or false if not
The next solution
You could override the stopPropagation
method on the base Event object and set your own flag in the overridden method like follows:
var isPropagationStopped = false;
var stopPropagationFuncTemp = Event.prototype.stopPropagation;
Event.prototype.stopPropagation = function()
{
isPropagationStopped = true;
stopPropagationFuncTemp.apply(this, arguments);
};
Now if isPropagationStopped == true
then it is stopped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With