我在Controller内部声明了messageBox。我想在onClose方法中使用全局变量。
我试图将其作为参数传递给onClose方法
var that =this;
sap.m.MessageBox.show(
"Notification " + odata.EvNotificationNo + " has been saved" + attachment_message,
{
icon: sap.m.MessageBox.Icon.SUCCESS,
title: "Success",
actions: [
"Go to Notification Processing",
sap.m.MessageBox.Action.OK,
sap.m.MessageBox.Action.CANCEL
],
onClose: function (sAction) {
//Here I have use var that
}
}
);
我想在onClose方法中使用该变量
答案 0 :(得分:1)
onClose方法内没有变量。
好像您在调试器在该方法中暂停时尝试访问该闭合变量。不幸的是,这在基于Chromium的浏览器中无法正常工作。参见Why does Chrome debugger think closed local variable is undefined?
当代码实际运行时,该变量将可用并经过正确评估。
除了上述问题之外,您还可以使用Function.prototype.bind
将上下文引用传递给事件处理程序,而不用尝试从方法中访问that
。
// var that = this; <-- instead of doing that
MessageBox.show("...", { // MessageBox required from "sap/m/MessageBox"
// ...,
onClose: function(sAction) {
// this.something instead of that.something
}.bind(this), // pass `this`
// ...
});
与上述闭包变量的问题相反,当调试器在此处暂停时,方法中始终可以使用上下文(this
),您可以从中访问分配给this
的任何属性
答案 1 :(得分:0)
在show函数外声明变量
var that = this;
var myGlobalVar; //Declare global variable
sap.m.MessageBox.show(
"Notification " + odata.EvNotificationNo + " has been saved" + attachment_message, {
icon: sap.m.MessageBox.Icon.SUCCESS,
title: "Success",
actions: ["Go to Notification Processing", sap.m.MessageBox.Action.OK, sap.m.MessageBox.Action.CANCEL],
onClose: function(sAction) {
myGlobalVar = "foo"; //Set global variable
if (sAction == "Go to Notification Processing") {
if (sap.ushell && sap.ushell.Container && sap.ushell.Container.getService) {
var oCrossAppNavigator = sap.ushell.Container.getService("CrossApplicationNavigation");
oCrossAppNavigator.toExternal({
target: {
semanticObject: "ZUI5_8FNPR_SEMR",
action: "execute"
}, //the app you're navigating to
params: {
EvNotificationNo: odata.EvNotificationNo
}
});
} else {
jQuery.sap.log.info("Cannot Navigate - Application Running Standalone");
}
}
}.bind(that)
}
);