gpt4 book ai didi

javascript - 从 Messagebox 内的同一 Controller 访问函数名称

转载 作者:行者123 更新时间:2023-11-28 12:59:54 29 4
gpt4 key购买 nike

我有一个消息框。

MessageBox.show(sMessageText, {
icon: icon ? icon : MessageBox.Icon.NONE,
title: stitle ? stitle : "",
actions: actions ? actions : MessageBox.Action.CLOSE,
id: id ? id : "DefaultMessageBoxId",
details: sFinalText ? sFinalText : "Error",
styleClass: bCompact ? "sapUiSizeCompact" : "",
onClose: function (oAction) {
if(oAction === CLOSE)
{
this.okFunction();
}
},

当用户单击消息框中的“关闭”按钮时,我想调用名为“okFunction”的函数,该函数与消息框位于同一 Controller 文件中。

问题是,如果我在消息框之外尝试 this.okFunction,它效果很好。但是,在消息框的 onClose 方法内部,它显示“this.okFunction 不是函数”。如何在 onClose 方法上调用函数?

谢谢。

最佳答案

只需使用 bind(this) 将上下文绑定(bind)到匿名函数

onClose: function (oAction) {
if(oAction === CLOSE)
{
this.okFunction();
}
}.bind(this)

这是一个工作片段

<!DOCTYPE html>
<html>

<head>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta charset="utf-8">

<title>MVC with XmlView</title>

<!-- Load UI5, select "blue crystal" theme and the "sap.m" control library -->
<script id='sap-ui-bootstrap' src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js' data-sap-ui-theme='sap_bluecrystal' data-sap-ui-libs='sap.m' data-sap-ui-xx-bindingSyntax='complex'></script>

<script id="view1" type="sapui5/xmlview">
<mvc:View controllerName="myController" xmlns:l="sap.ui.layout" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns:f="sap.ui.layout.form" xmlns="sap.m">
<Button text="show messageBox" press="onPress"></Button>
</mvc:View>
</script>


<script>
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/m/MessageBox"
], function(Controller, MessageBox) {
"use strict";

return Controller.extend("myController", {
onPress: function() {
var sMessageText = "Message Text";
var icon, stitle, actions, id, sFinalText, bCompact;
MessageBox.show(sMessageText, {
icon: icon ? icon : MessageBox.Icon.NONE,
title: stitle ? stitle : "",
actions: actions ? actions : MessageBox.Action.CLOSE,
id: id ? id : "DefaultMessageBoxId",
details: sFinalText ? sFinalText : "Error",
styleClass: bCompact ? "sapUiSizeCompact" : "",
onClose: function(oAction) {
if (oAction === "CLOSE") {
this.okFunction();
}
}.bind(this),
});
},

okFunction() {
alert("okFunction was executed")
}
});
});

// instantiate the View
var myView = sap.ui.xmlview({
viewContent: jQuery('#view1').html()
}); // accessing the HTML inside the script tag above

// put the View onto the screen
myView.placeAt('content');
</script>

</head>

<body id='content' class='sapUiBody'>
</body>

</html>

关于javascript - 从 Messagebox 内的同一 Controller 访问函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51631414/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com