Hi, I have an event 'change #year: function(evt). This gets called on the change of the dropdown, but I would also like to be able to call it from another event when a button is clicked. How would I call the ‘change #year’ event from my click event? I’ve not seen an example of calling an event from another event. Any suggestions?
You can add both selectors in your eventmap:
Template.example.events({
'change #year, click button'(event, instance) {
// do things
}
});
Alternatively extract the action into an instance function:
Template.example.onCreated(function() {
this.doSomething = function() {
// do something
};
});
Template.example.events({
'change #year'(event, instance) {
instance.doSomething();
},
'click button'(event, instance) {
instance.doSomething();
}
});
1 Like
Thanks. I tried the first example already, but didn’t get the result I was looking for. It looks like your second example should do the trick. Thank you!
Curious, in your second example, is there a benefit to putting the function inside the onCreated
versus just declaring the function inside the template file, for example:
// template.html
var doSomething = function() {
// do some stuff
}
Template.example.events({
'change #year'(event, instance) {
doSomething();
}
)};
Just that it’s scoped to the instance and can access template instance properties and methods. But you can just pass the instance in as a parameter, so it’s just personal preference really.
1 Like