-4

I want to change the compact density with a button.

onInit: function () {
  this.getView().addStyleClass("sapUiSizeCompact");
},

works well. If I will change it in a button, it does not work.

onOpenDialog: function (oEvent) {
  var oDialog1 = new Dialog();
  // ...
  oDialog1.addButton(new Button({
    text: "OK",
    press: function () {
      // How can I point to the view from inside this function?
    }
  }));
  oDialog1.open();
},

How can I point to the view from inside the function?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
EntePankopf
  • 23
  • 1
  • 6

1 Answers1

1

I'm surprised you had to ask, since it's basic Javascript, really...

Since press is invoked from oToggleButton, the this keyword now holds a reference to oToggleButton, and not the controller (hence why the getView() method fails). Search for 'this keyword in inner function' for more info.

To solve this, simply add a reference to this outside the inner function:

var that = this;

and in your inner function, use the reference instead:

that.getView().addStyleClass("sapUiSizeCompact");

Qualiture
  • 4,900
  • 7
  • 27
  • 38