Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify 'Must be attached' error to be more descriptive #376

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/blaze/domrange.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would propose to add some fallback string in case we can't resolve the name:

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.`)
});
}