I’ve recently implemented Stimulus on a project and I’m still trying to figure out when to use it, and when to just use regular JS/JQuery. One example of this is I have many tables throughout my app which have checkboxes at the start of each row. In the checkbox column in the table header I have a checkbox which checks/unchecks every checkbox in the table. Currently I’m doing this with a simple function attached to a class:
const bindAllChecks = function() {
$(".select-all-check").change(function() {
var checks = $(this).closest("table").find(".multiple-check");
if (this.checked) {
checks.prop("checked", true).change();
} else {
checks.prop("checked", false).change();
}
});
};
The Stimulus documentation seems to be encouraging me to move this into Stimulus, but that would involve not just adding a controller, but adding a data-controller
, data-target
and data-action
to every single instance of a checkbox table, which isn’t very DRY.
So my question is, is it worth using Stimulus for cases like this or is it better to mix Stimulus with plain JS files?