【问题标题】:Events are not being triggered when using Template.myTemplate() in Meteor在 Meteor 中使用 Template.myTemplate() 时未触发事件
【发布时间】:2015-08-11 06:09:14
【问题描述】:

我试图在谷歌地图信息窗口中捕捉点击按钮事件。为此,我有以下内容

<!-- Html file -->
<template name="infoWindowContent">
  <button>Click Me!</button>
</template>

然后我在 client.js 上有这个

//Client js file

//Function that renders the template. Gets called from a user's action
function showInfoWindow(map, marker) {
  var infoWindow = new google.maps.InfoWindow();
  //This is the line that generates the template's html
  var infoWindowHtmlContent = Template.infoWindowContent();
  infoWindow.setContent(infoWindowHtmlContent);
  infoWindow.open(map, marker);
}

//Template events
Template.infoWindowContent.events = {
  'click button': function(event){
    console.log('event was triggered', event);
  }
}

infoWindow 显示在地图上并包含按钮,但是当单击该按钮时,控制台中不会显示任何日志。对此有任何线索吗?如何捕获使用 Template.myTemplate() 呈现的某些元素调度的事件?

【问题讨论】:

    标签: google-maps meteor handlebars.js


    【解决方案1】:

    函数 Template.template() 返回纯 html 字符串。它没有启用任何 Meteor 属性。您需要的是一个注入了适当行为的 DOM 元素。

    ...
    var node = Meteor.render(Template.infoWindowContent());
    outerElement.appendChild(node);
    ...
    

    我不确定如何在google maps api中使用DOM节点,但如果可能的话,应该很容易。

     


     

    另外,使用适当的事件形式,从长远来看可以省去很多麻烦:

    Template.infoWindowContent.events({
      ...
    });
    

    【讨论】:

    • 嗨休伯特,感谢您的超快回复,并对我的延迟评论感到抱歉。我知道Meteor.render 方法在修改 DOM 元素时可以正常工作。但是它不适用于谷歌地图的infoWindow.setContent(content)。我收到一条错误消息Uncaught TypeError: Cannot read property 'firstChild' of null。有什么线索吗?
    猜你喜欢
    • 2013-08-23
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多