0

I instantiate this class in a different file by saying RunConfigurationController controller = new RunConfigurationController().

In the constructor, I make a new RunConfigurationView and then set the onclick functionality. Then when I run the program, and execute the cancel button click, I get an error saying that I cannot access Uncaught TypeError: Cannot read property 'getRunConfigurationModal' of undefined.

How can I access the "this" variable which I created in the constructor from another method inside the same class.

'use babel';

import RunConfigurationView from './run-configuration-view'

export default class RunConfigurationController {

  constructor() {
    this.runConfigurationView = new RunConfigurationView();
    this.runConfigurationView.getUpdateButton().onclick = this.updateButtonClick;
    this.runConfigurationView.getCancelButton().onclick = this.cancelButtonClick;

  }

  updateButtonClick() {

  }

  cancelButtonClick() {
    this.runConfigurationView.getRunConfigurationModal().remove();
  }

  getRunConfigurationView() {
    return this.runConfigurationView;
  }

}

1 Answers1

0

It's your binding...adjust these two lines in your constructor

this.runConfigurationView.getUpdateButton().onclick = this.updateButtonClick.bind(this);
this.runConfigurationView.getCancelButton().onclick = this.cancelButtonClick.bind(this);

See here or here for more info

sfletche
  • 47,248
  • 30
  • 103
  • 119