最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SAPUI5 - this.getView() is not a function - Stack Overflow

programmeradmin1浏览0评论

I am trying to get the input value, but when I call the function I get the error this.getView() is not a function

Below is the function in controller

    handleConfirmationMessageBoxPress: function(oEvent) {
        var bCompact = !!this.getView().$().closest(".sapUiSizeCompact").length;
        MessageBox.confirm(
            "Deseja confirmar a transferência?", {
                   icon: sap.m.MessageBox.Icon.SUCCESS,
                   title: "Confirmar",
                    actions: [sap.m.MessageBox.Action.OK, sap.m.MessageBox.Action.CANCEL],
                    onClose: function(oAction) {
                      if (oAction == "OK"){
                          var loginA = this.getView().byId("multiInput").getValue();
                          alert(loginA)
                          MessageToast.show("Transferência efetuada");

                      }else{
                         // MessageToast.show("Transferência não cancelada");
                           }

                        },
                        styleClass: bCompact? "sapUiSizeCompact" : ""
            }
        );
    }

And here is the input in the view

   <m:Input id="multiInput" value="teste" placeholder="Clique no botão ao lado para buscar o usuário" showValueHelp="true" valueHelpRequest="valueHelpRequest" width="auto"/>

I am trying to get the input value, but when I call the function I get the error this.getView() is not a function

Below is the function in controller

    handleConfirmationMessageBoxPress: function(oEvent) {
        var bCompact = !!this.getView().$().closest(".sapUiSizeCompact").length;
        MessageBox.confirm(
            "Deseja confirmar a transferência?", {
                   icon: sap.m.MessageBox.Icon.SUCCESS,
                   title: "Confirmar",
                    actions: [sap.m.MessageBox.Action.OK, sap.m.MessageBox.Action.CANCEL],
                    onClose: function(oAction) {
                      if (oAction == "OK"){
                          var loginA = this.getView().byId("multiInput").getValue();
                          alert(loginA)
                          MessageToast.show("Transferência efetuada");

                      }else{
                         // MessageToast.show("Transferência não cancelada");
                           }

                        },
                        styleClass: bCompact? "sapUiSizeCompact" : ""
            }
        );
    }

And here is the input in the view

   <m:Input id="multiInput" value="teste" placeholder="Clique no botão ao lado para buscar o usuário" showValueHelp="true" valueHelpRequest="valueHelpRequest" width="auto"/>
Share Improve this question asked May 3, 2017 at 14:44 Rubens CesarRubens Cesar 292 gold badges3 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I would assume that you get that error on the second this.getView() from inside the callback. You are getting this because of the way that this works in JavaScript. See the following MDN documentation: https://developer.mozilla/en/docs/Web/JavaScript/Reference/Operators/this.

In a nutshell, calling a function freely without it being referenced from "inside" an object (i.e. fnFunction() vs oObject.func()), will cause the this to point to either nothing or the window object. To get the right this, you can either use the arrow function declaration, the jQuery.proxy method or the .bind function:

onClose: oAction => {
   // your code
}

// OR

onClose: function(oAction) {
   // your code
}.bind(this)

// OR

onClose: jQuery.proxy(function(oAction) {
   // your code
}, this)
发布评论

评论列表(0)

  1. 暂无评论