this.getView()不起作用 - SAPUI5

Eva*_*ias 2 controller this sapui5

我一直试图this.getView()在我的控制器上使用几次,但我总是在我的控制台上得到getView不是函数错误.

有什么"技巧"或我可以做些什么来使这项工作?

Heres是我如何使用它的一个例子(这是我在我的控制器中创建的一个函数):

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

提前致谢!

Tim*_*ach 8

就在这里.如果您this在格式化程序函数中使用JSView ,并且事件侦听器指向它所属的Control.对于格式化程序函数,您可以通过分配此示例中的函数来更改此函数:

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

jQuery.proxy的帮助下,您可以this在其中myTextFormatter设置oController.这允许您this.getView()在格式化程序中调用,因为this现在将指向控制器.

为了设置事件侦听器的范围,您可以使用与上面所示相同的方式分配函数,也可以使用框架提供的不同方法,如下例所示:

new sap.m.Button({
    press : [oController.myPressHandler, oController]
});
Run Code Online (Sandbox Code Playgroud)

使用此表示法,框架将使用数组的第二个条目作为范围调用事件侦听器(myPressHandler).

此模式对于UI5框架中的大多数事件侦听器都有效.基本上,在分配事件侦听器时有三个选项:

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

使用XMLViews时,您不必手动设置范围,因为它默认设置为关联的控制器.