[英]Is there a way to call an event handler function to another event handler in Meteor?
I have these event handlers for my template: 我的模板具有以下事件处理程序:
Template.addPlayerForm.events({
'submit form': function(e) {
e.preventDefault();
var playerName = e.target.playerName.value;
var playerScore = e.target.playerScore.value;
if (playerName !== "" && playerScore !== "") {
Meteor.call('createPlayer',playerName,playerScore);
e.target.playerName.value = '';
e.target.playerScore.value = '';
}
},
'keypress .addScore': function(e) {
if (e.which >= 48 && e.which <= 57) {
return e.charCode;
}
else if (e.which === 13 || e.keycode === 13) {
e.preventDefault();
var playerName = e.target.playerName.value;
var playerScore = e.target.playerScore.value;
if (playerName !== "" && playerScore !== "") {
Meteor.call('createPlayer',playerName,playerScore);
e.target.playerName.value = '';
e.target.playerScore.value = '';
}
}
})
As you can see, the 'submit form'
and the 'keypress .addScore'
both have almost similar functions (ie adding a new collection to the database). 如您所见,
'submit form'
和'keypress .addScore'
都具有几乎相似的功能( 'keypress .addScore'
数据库添加新集合)。 My question is, is there a way that I can make a single function that can be passed to both event handlers? 我的问题是,有没有办法使我可以将一个函数传递给两个事件处理程序? I am new to Meteor and I am just practicing the online tutorial I saw, hoping to modify it as I see fit.
我是Meteor的新手,我只是在练习所见的在线教程,希望根据自己的喜好对其进行修改。 Thanks for the help!
谢谢您的帮助! :D
:D
You can create a function and call it inside the events 您可以创建一个函数并在事件内部调用它
Template.addPlayerForm.events({
'submit form': function(e) {
funcName(e)
},
'keypress .addScore': function(e) {
if (e.which >= 48 && e.which <= 57) {
return e.charCode;
}
else if (e.which === 13 || e.keycode === 13) {
funcName(e)
}
}
})
funcName = function(e){
e.preventDefault();
var playerName = e.target.playerName.value;
var playerScore = e.target.playerScore.value;
if (playerName !== "" && playerScore !== "") {
Meteor.call('createPlayer',playerName,playerScore);
e.target.playerName.value = '';
e.target.playerScore.value = '';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.