I’m trying to create a helper that returns the checked value (true or false) of a checkbox in order to display or not a select.
However the helper is returning the element as null even though it isn’t and in events it works.
I’m not sure how @shock would do it, but I’d accomplish it like this(May not be a good pattern):
Template.myTemplate.onCreated(function(){
this.linguaChecked = new ReactiveVar(null)
});
Template.myTemplate.events({
// Not sure of the exact syntax to listen for changes on an element
'changed .inputLa': function(event, template){
var checked = template.$('.inputLa').checked
template.linguaChecked.set(checked)
}
});
// Same way as above but just initial value, would combine into a function
Template.myTemplate.onRendered(function(){
// Get initial value
var checked = this.$('.inputLa').checked
this.linguaChecked.set(checked)
});
Template.myTemplate.helpers({
linguaChecked: function(){
return Template.instance().linguaChecked.get()
}
})
Yes, I was refering to template scope matching of $(’.inputLa’) as in example above.
It looks nice.
Maybe initialize that linguaChecked to something else than null based on html would be nice too. But than it need to be changed in both template+helper in case of another default value
If you define it as non-reactive variable and use it in helper, it would not rerun, so it will define just default.
Like
Template.myTemplate.onCreated(function(){
var defaultChecked = true;
this.linguaChecked = new ReactiveVar(defaultChecked)
});
Template.myTemplate.helpers({
defaultCheckedValue: return defaultChecked;
})