如何用Meteor js触发自定义事件 [英] How to trigger a custom event with Meteor js

查看:182
本文介绍了如何用Meteor js触发自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以用Meteor触发自定义事件吗?我看到触发自定义jquery事件不起作用,因为Meteor事件与jQuery分开(如这里所讨论的)。



所以如果我有这样的东西:

  Template.foo.events({
'mouseenter .box,makeSelected .box':function(){...}
})

如果我可以按照以下方式做一些事情会很好:


$ b $ ($('。box')[0],'makeSelected')

我目前的解决方法是将我想要的ID存储在 data-id ={{_ id}}上dom元素,然后使用它修改Session中的一个键,但是能够触发事件感觉更多的是DRY。

解决方案

p> Meteor现在似乎不支持自定义事件,但是你总是可以使用jQuery(或任何你想要的)创建自定义事件,然后确保它们在模板上使用呈现事件重新附加到各自的元素上:

  Template.foo.rendered = function(){
attachEvents();
}


Is it possible to trigger a custom event with Meteor? I see that triggering custom jquery events doesn't work, since the Meteor events are separate from jQuery (as discussed here).

So if I had something like:

Template.foo.events({
    'mouseenter .box, makeSelected .box': function() { ... }
})

It'd be nice if I could do something along the lines of:

Meteor.trigger($('.box')[0], 'makeSelected')

My current workaround is to just store the id that I want as data-id="{{_id}}" on the dom element and then use that to modify a key in the Session, but being able to trigger the event feels more "DRY".

解决方案

Meteor doesn't seem to support custom events at the moment, but you can always just use jQuery (or whatever you want) to create custom events, and then make sure they're re-attached to their respective elements with the rendered event on Templates:

Template.foo.rendered = function() {
  attachEvents();
}

这篇关于如何用Meteor js触发自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆