繁体   English   中英

MeteorJS-在template.onRendered之后单击事件

[英]MeteorJS - event click after template.onRendered

我有一个模板,希望通过Meteor.template.event()向其中添加click事件。 问题是.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");
  }
});

希望我有道理。 目前,控制台在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM