I’m able to used oEvent
when using this code:
JavaScript
x
9
1
onPressDialog: function(oEvent) {
2
if (!this._oDialog) {
3
this._oDialog= sap.ui.xmlfragment("idDialog", "com.Dialog", this);
4
this.getView().addDependent(this._oDialog);
5
}
6
this._oDialog.setBindingContext(oEvent.getSource().getParent().getBindingContext());
7
this._oDialog.open();
8
},
9
However, I’m trying to change it using Fragment.load
but I’m not able to get the oEvent
from the function. Any idea?
JavaScript
1
14
14
1
onPressDialog: function(oEvent) {
2
if (!this._oDialog) {
3
Fragment.load({ // Fragment required from "sap/ui/core/Fragment"
4
id: this.getView().getId(),
5
name: "com.Dialog",
6
controller: this
7
}).then(function(oDialog) {
8
this.getView().addDependent(oDialog);
9
oDialog.setBindingContext(/*Can't access the right oEvent values here*/);
10
oDialog.open();
11
}.bind(this));
12
}
13
},
14
Advertisement
Answer
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.
Given <Dialog id="myDialog">
in the fragment definition:
Since UI5 1.93
Using the API oController.loadFragment
JavaScript
1
7
1
onPressDialog: async function(oEvent) {
2
const myEventValue = oEvent.get/*...*/; // to use later without relying on oEvent
3
const oDialog = this.byId("myDialog") || await this.loadFragment({ name: "com.Dialog" });
4
// ... Do something with myEventValue ...
5
oDialog.open();
6
},
7
Since UI5 1.58
Using the API Fragment.load
JavaScript
1
15
15
1
onPressDialog: async function(oEvent) {
2
const myEventValue = oEvent.get/*...*/; // to use later without relying on oEvent
3
let oDialog = this.byId("myDialog");
4
if (!oDialog) {
5
oDialog = await Fragment.load({ // Fragment required from "sap/ui/core/Fragment"
6
id: this.getView().getId(),
7
name: "com.Dialog",
8
controller: this,
9
});
10
this.getView().addDependent(oDialog);
11
}
12
// ... Do something with myEventValue ...
13
oDialog.open();
14
},
15