使用`Fragment.load`时如何使用`oEvent`? [英] How to use `oEvent` when using `Fragment.load`?

查看:27
本文介绍了使用`Fragment.load`时如何使用`oEvent`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm able to used oEvent when using this code:

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.有什么想法吗?

However, I'm trying to change it using Fragment.load but I'm not able to get the oEvent from the function. Any idea?

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));
    }
},

推荐答案

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

As explained in the linked answer above, the oEvent parameters are completely reset after the event handler (onPressDialog) is executed. I.e. after the fragment is fetched asynchronously, the oEvent object won't contain the same references / parameter values anymore. Try storing the target reference in a closure variable before creating the fragment, and then use the variable when the promise is finally resolved.

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

这篇关于使用`Fragment.load`时如何使用`oEvent`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆