异步加载分片时如何使用`oEvent`?
我可以在使用此代码时使用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所述,在执行事件处理程序(onPressDialog
)后,oEvent
参数完全重置。即异步获取片段后,oEvent
对象将不再包含相同的引用/参数值。尝试在创建片段之前将目标引用存储在闭包变量中,然后在最终解析承诺时使用该变量。
片段定义中给定的<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();
},
相关文章