From 85c24b97f2a663ef9cd44f3da2a28b8f9613f4f8 Mon Sep 17 00:00:00 2001 From: harryadel Date: Sat, 18 Jun 2022 20:23:31 +0200 Subject: [PATCH 1/3] [blaze] Modify 'Must be attached' error Now it outputs a more descriptive version: `${event} event triggerd with ${selector} on ${templateName} but associated view is not be found. Make sure the event doesn't destroy the view.` Fixes https://github.com/meteor/blaze/issues/213 --- packages/blaze/domrange.js | 6 ++++-- packages/blaze/view.js | 2 +- packages/blaze/view_tests.js | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/blaze/domrange.js b/packages/blaze/domrange.js index ca39ac459..216d434ae 100644 --- a/packages/blaze/domrange.js +++ b/packages/blaze/domrange.js @@ -332,9 +332,11 @@ 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.split('.')[1]; 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.`) + }); } From dc2a520d2c3892460d92b4aa03295b479f662e24 Mon Sep 17 00:00:00 2001 From: harryadel Date: Sun, 19 Jun 2022 09:25:37 +0200 Subject: [PATCH 2/3] [blaze] Add guarding for templateName --- packages/blaze/domrange.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/blaze/domrange.js b/packages/blaze/domrange.js index 216d434ae..4be7662ee 100644 --- a/packages/blaze/domrange.js +++ b/packages/blaze/domrange.js @@ -333,7 +333,7 @@ DOMRange.prototype.destroy = function (_skipNodes) { }; DOMRange.prototype.containsElement = function (elem, selector, event) { - const templateName = this.view.name.split('.')[1]; + const templateName = this.view.name?.split('.')[1]; if (! this.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.`); From 334130c26b320e4e6a75b1a32519405867eb3fa2 Mon Sep 17 00:00:00 2001 From: harryadel Date: Sun, 19 Jun 2022 09:35:50 +0200 Subject: [PATCH 3/3] [blaze] Add 'unkown template' as a fallback to templateName --- packages/blaze/domrange.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/blaze/domrange.js b/packages/blaze/domrange.js index 4be7662ee..f9baa465f 100644 --- a/packages/blaze/domrange.js +++ b/packages/blaze/domrange.js @@ -333,7 +333,9 @@ DOMRange.prototype.destroy = function (_skipNodes) { }; DOMRange.prototype.containsElement = function (elem, selector, event) { - const templateName = this.view.name?.split('.')[1]; + const templateName = this.view?.name + ? this.view.name.split('.')[1] + : 'unknown template'; if (! this.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.`);