Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen for click events on the whole page in meteor?

I'm experimenting with the leaderboards example and I would like to unset the selected_player when you click outside a player name. I figured for this to work, I'd need to add a listener event to the body element and prevent it from triggering other elements that are inside it. Then I would set selected_player to 0.

However I only seem to be able to add event maps to Templates?

  Template.player.events({
    'click': function () {
      Session.set("selected_player", this._id);
    }
  });

Am I missing something? How can I listen to a 'click' event for the whole page?

like image 526
Nick Avatar asked Nov 08 '12 21:11

Nick


1 Answers

There is no good way to bind events to the whole body. However, you can wrap all your code in one outer template that has events, as Narven suggests.

<body>
  {{> outer}}
</body>

<template name="outer">
   your stuff
</template>

and

Template.outer.events({
  'click': function () {
    do stuff 
  }
});
like image 69
n1mmy Avatar answered Nov 14 '22 12:11

n1mmy