流星如何创建全球事件?

sas*_*dev 12 meteor

如何创建可在任何页面上运行的事件?

在主布局模板上创建帮助程序时,它不起作用.

Template.layout.events
  'click': ->
    console.log "you clicked on the site"
Run Code Online (Sandbox Code Playgroud)

Aks*_*hat 15

您可以使用Template.body.events而不是Template.layout.events为任何适用于任何内容的模板创建事件<body>

文档:http://docs.meteor.com/#/full/template_body

  • 这个答案是对的.结帐我的 (2认同)

Dud*_*ude 6

你需要一个特殊的包来创建一个全局事件.

安装

meteor add gwendall:body-events
Run Code Online (Sandbox Code Playgroud)

并且您可以使用Template.body.events每个模板中的事件

Template.body.events({
   'click .myClass':function(){
       alert("BODY EVENT");
   }
});
Run Code Online (Sandbox Code Playgroud)

或者如果你喜欢新的语法

Template.body.events({
   'click .myClass'(){
       alert("BODY EVENT");
   }
});
Run Code Online (Sandbox Code Playgroud)