Skip to content

Commit

Permalink
fix(DOMRange): Modify 'Must be attached' error to be more descriptive;
Browse files Browse the repository at this point in the history
…fix #213

Merge pull request #376 from harryadel/view-must-be-attached-error
Thanks to @harryadel
  • Loading branch information
jankapunkt authored Jun 22, 2022
2 parents 5f85a72 + 334130c commit e6bf934
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/blaze/domrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/blaze/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions packages/blaze/view_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`)
});
}

0 comments on commit e6bf934

Please sign in to comment.