【问题标题】:Meteor - collection find after global keydown?流星 - 全局按键后找到集合?
【发布时间】:2014-07-02 03:41:10
【问题描述】:
我搜遍了,Meteor 好像不能同时做这两件事。
我想要的很简单:当(全局)按下任何按钮时,会调用一个 collection.find(...) 。它在 Template.events 中是不可行的,因为 Meteor 不支持全局按键,我不能在 Template.rendered 中这样做,因为由于某种原因,collection.find 总是什么都不返回。
有什么想法吗?
【问题讨论】:
标签:
javascript
templates
meteor
【解决方案1】:
以下工作,但需要页面中的一个元素具有焦点,我猜这是你的问题。
UI.body.events({
'keydown': function(event, template){
console.log('A key is down!')
}
})
我猜你必须在没有 Meteor 的情况下这样做。以下内容应适用于现代浏览器:
function keydownListener(event){
console.log('A key is down!')
}
Template.templateName.created = function(){
document.body.addEventListener('keydown', keydownListener)
}
Template.templateName.destroyed = function(){
document.body.removeEventListener('keydown', keydownListener)
}