mercredi 22 septembre 2021

How can I call a method from inside another method without getting a ReferenceError?

I have the following method in a StimulusJS controller that sets the state of a "master" checkbox depending on the children checkboxes' state.

I'm trying to refactor it and move the arrow function behaviour to a private method but I'm getting ReferenceErrors when calling the private method from the main one.

I've tried declaring the private method within the main one to no success.

What is the correct way to do it?

main function:

  updateSelectAllCheckboxState() {
    let selectAllCheckBox = this.selectAllTarget
    const allChecked = (checkbox) => checkbox.checked; // I want to move this to a private method

    if (allChecked()) {
      selectAllCheckBox.indeterminate = false
      selectAllCheckBox.checked = true
    }
  }

refactor. I get ReferenceError: allChecked is not defined:

  updateSelectAllCheckboxState() {
    let selectAllCheckBox = this.selectAllTarget

    if (allChecked()) {
      selectAllCheckBox.indeterminate = false
      selectAllCheckBox.checked = true
    }
  }


// private

  allChecked() {
    const allChecked = (checkbox) => checkbox.checked;
    
    if (this.checkboxTargets.every(allChecked) {
      return true
    }
  }



Aucun commentaire:

Enregistrer un commentaire