简体   繁体   English

如何在Meteor JS中检测整个文档的事件?

[英]How to detect events on entire document in Meteor JS?

It's really as simple as that. 它真的很简单。 I currently have in my template events, but it is not working: 我目前在我的模板事件中,但它不起作用:

 'keyup': function(event) {
    event.preventDefault();
    console.log("KEYUP");
}

Update: it appears to be a generic bug in the framework. 更新:它似乎是框架中的一般错误。 I would appreciate it if anyone could share a manual hack I could employ that doesn't involve using someone else's packages. 我很感激,如果有人可以分享我可以使用的手动黑客,不涉及使用别人的包。

Update 2: I have solved this problem as shown below. 更新2:我已经解决了这个问题,如下所示。

Unfortunately there is a bug in meteor ( source ) that events don't fire on UI.body.template . 不幸的是,流星( )中存在一个错误,即事件不会在UI.body.templateUI.body.template You can use a package meteor-body-events to patch that, and attach keyup event on entire document. 您可以使用包meteor-body-events来修补它,并在整个文档上附加keyup事件。

All of a sudden my original code works. 我的原始代码突然起作用了。 Maybe the good folks at Meteor has fixed the problem, yay! 也许Meteor的优秀人员已经解决了这个问题,耶!

Here's what works for me on 0.9.3.1: 以下是0.9.3.1对我有用的内容:

Template.template_name_here.events({
  'keyup': function(event) {
    event.preventDefault();
    console.log("KEYUP");
  }
});

Meteor can even handle multiple events with a single handler like this: Meteor甚至可以使用单个处理程序处理多个事件,如下所示:

Template.template_name_here.events({
  'keyup, click': function(event) {
    event.preventDefault();
    console.log("KEYUP OR CLICK");
  }
});

Awesome! 真棒!

Currently Meteor is properly handling <body> events, unless you are using a renderer which takes over rendering of the template. 目前Meteor正在正确处理<body>事件, 除非您使用的渲染器接管模板的渲染。 An example of this is Kadira:blaze-templates which will override default Meteor rendering of template (for use with FlowRouter), or Iron Router . 这方面的一个例子是Kadira:blaze-templates ,它将覆盖模板的默认Meteor渲染(用于FlowRouter)或Iron Router

Luckily there is a simple package to restore this functionality called gwendall:body-events . 幸运的是,有一个简单的包来恢复这个功能,称为gwendall:body-events

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM