diff --git a/packages/blaze/domrange.js b/packages/blaze/domrange.js index ca39ac459..f9baa465f 100644 --- a/packages/blaze/domrange.js +++ b/packages/blaze/domrange.js @@ -332,9 +332,13 @@ DOMRange.prototype.destroy = function (_skipNodes) { DOMRange._destroy(this, _skipNodes); }; -DOMRange.prototype.containsElement = function (elem) { +DOMRange.prototype.containsElement = function (elem, selector, event) { + const templateName = this.view?.name + ? this.view.name.split('.')[1] + : 'unknown template'; if (! this.attached) - throw new Error("Must be attached"); + throw new Error(`${event} event triggerd with ${selector} on ${templateName} but associated view is not be found. + Make sure the event doesn't destroy the view.`); // An element is contained in this DOMRange if it's possible to // reach it by walking parent pointers, first through the DOM and diff --git a/packages/blaze/view.js b/packages/blaze/view.js index 413bb4ab7..185197e62 100644 --- a/packages/blaze/view.js +++ b/packages/blaze/view.js @@ -895,7 +895,7 @@ Blaze._addEventMap = function (view, eventMap, thisInHandler) { handles.push(Blaze._EventSupport.listen( element, newEvents, selector, function (evt) { - if (! range.containsElement(evt.currentTarget)) + if (! range.containsElement(evt.currentTarget, selector, newEvents)) return null; var handlerThis = thisInHandler || this; var handlerArgs = arguments; diff --git a/packages/blaze/view_tests.js b/packages/blaze/view_tests.js index c9f271723..fe1796a1d 100644 --- a/packages/blaze/view_tests.js +++ b/packages/blaze/view_tests.js @@ -56,4 +56,9 @@ if (Meteor.isClient) { test.equal(buf, ""); }); + Tinytest.add("blaze - view - attached", function (test) { + test.throws(() => Blaze._DOMRange.prototype.containsElement.call({attached: false, view: {name: 'Template.foo'}}, undefined, '.class', 'click'), + `click event triggerd with .class on foo but associated view is not be found. + Make sure the event doesn't destroy the view.`) + }); }