2

I've been trying to use this.getView() on my controllers a couple of times but I'm always getting getView is not a function error on my console.

Is there any "trick" or something that I can do to make this work?

Heres's an example of how I'm using it (this is a function I've created in my controller):

formatIconStatus: function(status){
         var view = this.getView();
         if (status != null){
             if (status == "YES"){
                 status = "sap-icon://accept";
             }else{
                 status = "sap-icon://error";
             }
             return status;
         }
     },

Thanks in advance!

Eva Dias
  • 1,709
  • 9
  • 36
  • 67
  • Try a `console.log("this", this);` as first command in the function to check what `this` actually is. – herrlock Aug 06 '15 at 16:17
  • this M.c…s.f {bAllowTextSelection: true, mEventRegistry: Object, sId: "__status14-__table0-0", mProperties: F, mAggregations: Object…} - This function is being called in a table formatter for a status formatter. So, the "this" is point to that cell. – Eva Dias Aug 06 '15 at 16:23

1 Answers1

8

Yes, there is. If you are using a JSView this in formatter functions and event listeners points to the Control it belongs to. For formatter functions you can change this by assigning the function like in this example:

new sap.m.Button({
    text : {
        path : "/buttonText",
        formatter : $.proxy(oController.myTextFormatter, oController);
    }
});

With the help of jQuery.proxy you can set this within myTextFormatter to oController. This allows you to call this.getView() within your formatter since this will now point to the controller.

In order to set the scope for event listeners you can either assign the function the same way as shown above or you can use a different approach offered by the framework like in the following example:

new sap.m.Button({
    press : [oController.myPressHandler, oController]
});

Using this notation, the framework will call the event listener (myPressHandler) with the second entry of the array as scope.

This pattern is valid for most event listeners across the UI5 framework. Basically, you have three options when assigning event listeners:

  • fnListenerFunction
  • [fnListenerFunction, oListenerScope]
  • [oData, fnListenerFunction, oListenerScope]

Using XMLViews you don´t have to set the scope manually as it´s set to the associated controller by default.

Tim Gerlach
  • 3,390
  • 3
  • 20
  • 39