I can disable the following button from the console via $("#myButton").prop('disabled', true);
However, when I do this in Template.myTemplate.events it does not work:
<template name = "myTemplate">
<button id="myButton"></button>
</template>
I am now using Session variables, because in my case it makes sense to disable all buttons when one is clicked (dont want other user interactions while one action is performing) - but I am not sure if this a good approach:
Template.myTemplate.events({
'click #myButton': function (event, instance) {
$("#myButton").prop('disabled', true); //why does this not work?
event.preventDefault();
//Do my stuff and enable button click again
}
<template name = "myTemplate">
<button id="myButton" {{buttonStateDisabled}} ></button>
</template>
your template wouldnât work this way because Spacebars/Blaze treats âdisabledâ (and other standard attributes) in a special way.
hereâs how it should be:
donât use Session variables for something that is well contained within a template
donât use helpers to mutate state (i.e. your setTimeout callback), especially in the case where multiple elements use the same helper. this would redundantly trigger your timeout callback
It should works. Spacebars (Blaze) affects properties that have been defined for templates. In a Blaze documentation written that Blaze do not touch changes have been maden by the hand (or with jQuery) - it means Blaze canât override or clear disabled attribute.
I use such way too and it works as expected. Next I guess $("#myButton").prop('disabled', true) doesnt works. You could try this one
Template.myTemplate.events({
'click #myButton': function (event, instance) {
$(event.currentTarget).prop('disabled', true);
}