简体   繁体   English

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

[英]MeteorJS - event click after template.onRendered

I've got a template that I want to add a click event to with Meteor.template.event(). 我有一个模板,希望通过Meteor.template.event()向其中添加click事件。 Problem is that the .event() is not working right and I can't get a hold of the DOM. 问题是.event()无法正常工作,我无法掌握DOM。

Im using Meteor.template.onRendered() to display API data and I'm using jQuery to shove the data into the template. 我正在使用Meteor.template.onRendered()来显示API数据,而我正在使用jQuery将数据推送到模板中。 Then I want to grab a [name] selector that was generated through jQuery and manipulate it with Meteor.template.event() but it's not working 然后,我想获取一个通过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");
  }
});

Hope I made sense. 希望我有道理。 At the moment the console log in the events() function isn't working either. 目前,控制台在events()函数中的日志也不起作用。 I'm pretty sure it's because of the DOM not "existing" and JS can't get a hold of it. 我很确定这是因为DOM不存在而JS无法获得它。

Alright so I found a workaround to this problem and it works. 好的,所以我找到了解决此问题的方法,并且它可以工作。 Which is all I want at the moment. 目前我要的是哪一个。 I overlooked Meteor.events() completely and worked through the Meteor.onRendered() block. 我完全忽略了Meteor.events()并通过Meteor.onRendered()块进行工作。

I made a function upsertNPoints() that "calls" my declared method on the server and uses it to upsert points to my points table. 我制作了一个函数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();

Hope this helps any one with this weird issue I had. 希望这对我遇到的这个怪异问题有帮助。 Thanks for your suggestions @Petr and @chazsolo 感谢您的建议@Petr和@chazsolo

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

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