I'm trying to catch any click event inside the window from inside a template MyTemplate
, with something like this:
Template.MyTemplate.events({
'click' : function(e) {
console.log("click");
// Here I need to access Template.instance()
// ...
}
});
But the code above doesn't works as I would like but catches only clicks inside the template's DOM and I can't get clicks outside the template.
Also, I would like to have the code inside the template itself, so something like Template.body.events({ ... })
is not an option.
WHAT I TRIED
I already tried this solution:
function clickEvent(event) {
console.log('click');
// Here I can access the right `Template.instance()` as `this`
// thanks to the bind below
// ...
}
Template.MyTemplate.onCreated(function () {
// The `bind` give me the template instance inside the `clickEvent`
// function
$(document).on('click', clickEvent.bind(this));
});
Template.MyTemplate.onDestroyed(function () {
$(document).off('click', clickEvent);
});
It works but seems like a workaround: I have all events inside Template.MyTemplate.events({ ... })
while these ones are outside, I need to do a bind to get the template's instance and it uses JQuery to bind events and not Meteor itself.
So, is there no a simpler-way/Meteor-way to do that? Like some code that can go inside Template.MyTemplate.events({ ... })
?