I have a function which needs to attach and detach a handler to the event “requestCompleted” of the OData model to get the URL from the header in order to download the data as an Excel file.
JavaScript
x
15
15
1
onClickAction: function (oEvent) {
2
var model = this.getView().getModel();
3
model.attachRequestCompleted(this.downloadODataAsExcel);
4
var btnGo = this.getView().byId("btn");
5
btnGo.firePress();
6
model.detachRequestCompleted(this.downloadODataAsExcel, this);
7
},
8
9
downloadODataAsExcel: function (evt) {
10
var url;
11
url = evt.getParameters() && evt.getParameters().url;
12
url = "/sap/opu/odata/sap/ZService/" + url + "&$format=xlsx";
13
sap.m.URLHelper.redirect(url, true);
14
},
15
I am trying to detach the event afterwards to prevent snowballing of the event which causes the file to download n+1 times each time you click the download button.
Update: this is the code I ended up with
JavaScript
1
16
16
1
onClickAction: function (oEvent) {
2
var model = this.getView().getModel();
3
model.attachRequestCompleted(this.downloadOdataAsExcel, this);
4
var btnGo = this.getView().byId("btn");
5
btnGo.firePress();
6
},
7
8
downloadODataAsExcel: function (evt) {
9
var url;
10
url = evt.getParameters() && evt.getParameters().url;
11
url = "/sap/opu/odata/sap/Z_SERVICE/" + url + "&$format=xlsx";
12
sap.m.URLHelper.redirect(url, true);
13
var model = this.getView().getModel();
14
model.detachRequestCompleted(this.downloadODataAsExcel, this);
15
}
16
The detach needs to be within the function otherwise the listener will be detached before the requestCompleted
event fires.
Advertisement
Answer
Try this:
model.attachRequestCompleted(this.downloadOdataAsExcel, this);
And then try to access the ‘evt’ object.