I’m creating a custom date and time picker that I’d like to use in a couple different templates. After the user picks a date, I’d like to call a method with the date to insert a new item into a collection. The method I call depends on which parent template the child lives in.
I’m using Blaze. Right now, I have it working by using a Reactive Var. The parent passes the Reactive Var to the child. The child sets the Reactive Var and the parent gets it inside an autorun that’s in the parent’s onCreated
. Then I have some if
blocks to determine if I should call the method or not.
Wondering if there’s a better way to approach this. Any ideas?
// parent.js
Template.parent.onCreated(function() {
const instance = this;
instance.date = new ReactiveVar();
instance.autorun(function() {
const date = instance.date.get();
if (date) {
// call certain Meteor.method to insert
}
if (date === null) {
// call certain Meteor.method to remove
}
});
});
});
Template.parent.helpers({
dateRV: function() {
return Template.instance().date;
},
});
// parent.html
{{> child date=dateRV}}
// child.js
Template.child.events({
'click .done': function(event, template) {
// parse the input to get the date
template.data.date.set(parsedDate);
},
'click .reset': function(event, template) {
template.data.date.set(null);
}
});