gpt4 book ai didi

javascript - 异步加载片段时如何使用 `oEvent`?

转载 作者:行者123 更新时间:2023-12-03 07:04:49 24 4
gpt4 key购买 nike

使用此代码时,我可以使用 oEvent:

onPressDialog: function(oEvent) {
if (!this._oDialog) {
this._oDialog= sap.ui.xmlfragment("idDialog", "com.Dialog", this);
this.getView().addDependent(this._oDialog);
}
this._oDialog.setBindingContext(oEvent.getSource().getParent().getBindingContext());
this._oDialog.open();
},

但是,我尝试使用 Fragment.load 更改它,但我无法从函数中获取 oEvent。有什么想法吗?

onPressDialog: function(oEvent) {
if (!this._oDialog) {
Fragment.load({ // Fragment required from "sap/ui/core/Fragment"
id: this.getView().getId(),
name: "com.Dialog",
controller: this
}).then(function(oDialog) {
this.getView().addDependent(oDialog);
oDialog.setBindingContext(/*Can't access the right oEvent values here*/);
oDialog.open();
}.bind(this));
}
},

最佳答案

linked answer 中所述上面,oEvent执行事件处理程序 (onPressDialog) 后,参数将完全重置。 IE。在异步获取片段后,oEvent对象将不再包含相同的引用/参数值。在创建片段之前尝试将目标引用存储在闭包变量中,然后在最终解决 promise 时使用该变量。

鉴于<Dialog id="myDialog"> 在片段定义中:

从 UI5 1.93 开始

使用 API oController.loadFragment

onPressDialog: async function(oEvent) {
const myEventValue = oEvent.get/*...*/; // to use later without relying on oEvent
const oDialog = this.byId("myDialog") || await this.loadFragment({ name: "com.Dialog" });
// ... Do something with myEventValue ...
oDialog.open();
},

从 UI5 1.58 开始

使用 API Fragment.load

onPressDialog: async function(oEvent) {
const myEventValue = oEvent.get/*...*/; // to use later without relying on oEvent
let oDialog = this.byId("myDialog");
if (!oDialog) {
oDialog = await Fragment.load({ // Fragment required from "sap/ui/core/Fragment"
id: this.getView().getId(),
name: "com.Dialog",
controller: this,
});
this.getView().addDependent(oDialog);
}
// ... Do something with myEventValue ...
oDialog.open();
},

关于javascript - 异步加载片段时如何使用 `oEvent`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64661249/

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