【问题标题】:Using 1 event handler for multiple templates对多个模板使用 1 个事件处理程序
【发布时间】:2017-11-05 14:50:16
【问题描述】:

我一直在使用通常的 Blaze Template.events 处理事件的方式。目前,我在多个模板中有许多相同的事件。

我很好奇是否有人知道一种方法来处理多个模板的事件,而不必为每个不同的模板使用多个 Template.events 调用。

(您可以只编写一个事件处理程序来应用于多个模板)。

如果我切换到 Meteor-React,我认为这可以做到,但我现在不想这样做。

【问题讨论】:

  • 我过去用dynamic templates 做过这个。或者您可以定义各种事件处理程序调用的通用函数。

标签: javascript meteor meteor-blaze


【解决方案1】:

你可以使用这个名为meteor-body-events的包

meteor add gwendall:body-events

他们的 git 页面是这样的:https://github.com/gwendall/meteor-body-events

我在以前的项目中使用过它,它按预期工作,但现在我看到该项目不再维护。我相信它应该仍能按预期工作。

来自他们的 git 页面的示例:

Template.body.events({
  "click .btn": function(e, data, tpl) {
    // e -> jquery event
    // data -> Blaze data context of the DOM element triggering the event handler
    // tpl -> the parent template instance for the target element
  }
})

因此,基本上,您只需将事件侦听器标记到主体一次,它就会适用于所有模板。

【讨论】:

  • 这正是我想要的,谢谢。希望它不会因为某种原因减慢很多东西。谢谢
猜你喜欢
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
相关资源
最近更新 更多