【问题标题】:Call controller function of SAP UI5 using jQuery使用jQuery调用SAP UI5的控制器函数
【发布时间】:2018-01-20 16:39:27
【问题描述】:

有没有办法在控制器的 init() 函数中编写的 jQuery 语法中调用 SAP UI5 控制器函数? 我的意思是我在 user.controller.js 的 init() 中写了下面的代码 sn-p。我有一个函数 checkId() 写在同一个控制器中,我想在下面的 sn-p 中调用它。如何做到这一点? 我的代码:

    $(window).load(function(){
       $("#myid").mousedown(function(){
         this.checkId(); //returns is not a function
       });
    });

【问题讨论】:

标签: jquery sapui5


【解决方案1】:

这与 this 关键字在 JavaScript 中的工作方式密切相关。查看How does the "this" keyword work?了解更多详情。

您有三种不同的可能性来克服您面临的问题:

1) 给this 取别名(存储到变量中):

var oController = this;
$(window).load(function(){
    $("#myid").mousedown(function(){
         oController.checkId();
    });
});

2) 为您的内部函数重新绑定 this 上下文(使用例如 .bindjQuery.proxy):

$(window).load(function(){
    $("#myid").mousedown(function(){
         this.checkId();
    }.bind(this));
}.bind(this));

3) 使用ES6 arrow functions(如果您没有遇到与浏览器支持相关的问题或者您有转译构建设置):

$(window).load(() => $("#myid").mousedown(() => this.checkId()));

另外,您应该尽可能避免在 UI5 中手动添加 DOM 事件处理程序。您可以简单地使用 UI5 按钮并将您的方法附加到 UI5 press 事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    相关资源
    最近更新 更多