Javascript MeteorJS-template.onRendered后的事件单击

Javascript MeteorJS-template.onRendered后的事件单击,javascript,jquery,meteor,Javascript,Jquery,Meteor,我有一个模板,我想用Meteor.template.event()向其中添加一个单击事件。问题是.event()工作不正常,我无法获得DOM 我使用Meteor.template.onRendered()显示API数据,并使用jQuery将数据推送到模板中。然后我想获取一个通过jQuery生成的[name]选择器,并使用Meteor.template.event()对其进行操作,但它不起作用 //server Meteor.method() incrementNormalPoints: func

我有一个模板,我想用Meteor.template.event()向其中添加一个单击事件。问题是.event()工作不正常,我无法获得DOM

我使用Meteor.template.onRendered()显示API数据,并使用jQuery将数据推送到模板中。然后我想获取一个通过jQuery生成的[name]选择器,并使用Meteor.template.event()对其进行操作,但它不起作用

//server Meteor.method()
incrementNormalPoints: function(err){
  Points.upsert( {userId: this.userId}, { $inc : {normalPoints : 2} });
}

//client Meteor.template.onRendered().. this part works 
Template.normalMode.onRendered(function(){
  $("[name=imageGuess").append('<div class="flip"><div name="imageCard" class="card"><div class="face front"><img name="rightAnswer" id="theImg" class="img-responsive" src="'+imgArray[i]+'" /></div><div class="face back"><span class="center-glyphicon glyphicon glyphicon-ok"></div></div></div>');
}

Template.normalMode.events({
  "click [name=rightAnswer]" : function(){
     event.preventDefault();
     Meteor.call("incrementNormalPoints");
     console.log("hello");
  }
});
//服务器Meteor.method()
incrementNormalPoints:函数(错误){
upsert({userId:this.userId},{$inc:{normalPoints:2}});
}
//客户端Meteor.template.onRendered()。。这部分有效
Template.normalMode.onRendered(函数(){
$(“[name=imageGuess”)。追加(“”);
}
Template.normalMode.events({
“单击[name=rightAnswer]”:函数(){
event.preventDefault();
Meteor.call(“增量正常点”);
console.log(“你好”);
}
});

希望我讲得通。目前控制台日志中的events()函数也不起作用。我很确定这是因为DOM不“存在”,JS无法控制它。

好吧,所以我找到了解决这个问题的方法,它可以工作。这就是我目前想要的。我忽略了Meteor.events()完成并完成Meteor.onRendered()块

我创建了一个函数upsertNPoints(),该函数“调用”服务器上声明的方法,并使用它将点向上插入到我的点表中

//server Methods()
incrementNormalPoints: function(err){
Points.upsert( {userId: this.userId}, { $inc : {normalPoints : 2} });
if(err){
  console.log(err);
}
},

//Upsert database using the call() method meteor provides on the client
function upsertNPoints(){
  $("[name=rightAnswer]").on("click", function(){
    Meteor.call("incrementNormalPoints");
    location.reload(true);//this is just to reload the window after a click
  });
}

upsertNPoints();

希望这能帮助任何人解决我遇到的这个奇怪问题。感谢您的建议@Petr和@chazsolo

有没有办法通过Blaze在模板中插入HTML,而不是使用
onRendered
?为什么不为事件中要做的任何事情创建一个单独的函数,并从事件和从事件中调用该函数
onRendered
?@chazsolo我对meteor很陌生。我之所以使用jQuery,是因为我从正在使用的API随机生成图像,并将它们推到DOM中。它不是静态的。你知道我在哪里可以学到更多的链接吗?Petr你是说一个助手函数吗?我认为@Petr说的是使用
助手
来调用我不知道这是否是个好主意,但如果你们去我的网站,你们会看到我的代码。我使用一种算法将API中的图像随机化,将它们放入数组,然后用jquery将它们放入模板中。我不确定这是否是你想要的答案为王