异步加载片段时如何使用`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));

    }

},


哆啦的时光机
浏览 140回答 1
1回答

Cats萌萌

oEvent在事件处理程序(onPressDialog)执行后,参数被完全重置。即异步获取片段后,该oEvent对象将不再包含相同的引用/参数值。尝试在创建片段之前将目标引用存储在闭包变量中,然后在最终解决 Promise 时使用该变量。<Dialog id="myDialog">片段定义中给出:从 UI5 1.93 开始使用APImyController.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 开始使用APIFragment.loadonPressDialog: async function(oEvent) {  const myEventValue = oEvent.get/*...*/; // to use later without relying on oEvent  let oDialog = this.byId("myDialog");  this.getOwnerComponent().runAsOwner(function() {    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();  }.bind(this));},
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript