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.
onClickAction: function (oEvent) {
var model = this.getView().getModel();
model.attachRequestCompleted(this.downloadODataAsExcel);
var btnGo = this.getView().byId("btn");
btnGo.firePress();
model.detachRequestCompleted(this.downloadODataAsExcel, this);
},
downloadODataAsExcel: function (evt) {
var url;
url = evt.getParameters() && evt.getParameters().url;
url = "/sap/opu/odata/sap/ZService/" + url + "&$format=xlsx";
sap.m.URLHelper.redirect(url, true);
},
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
onClickAction: function (oEvent) {
var model = this.getView().getModel();
model.attachRequestCompleted(this.downloadOdataAsExcel, this);
var btnGo = this.getView().byId("btn");
btnGo.firePress();
},
downloadODataAsExcel: function (evt) {
var url;
url = evt.getParameters() && evt.getParameters().url;
url = "/sap/opu/odata/sap/Z_SERVICE/" + url + "&$format=xlsx";
sap.m.URLHelper.redirect(url, true);
var model = this.getView().getModel();
model.detachRequestCompleted(this.downloadODataAsExcel, this);
}
The detach needs to be within the function otherwise the listener will be detached before the requestCompleted
event fires.
Try this:
model.attachRequestCompleted(this.downloadOdataAsExcel, this);
And then try to access the 'evt' object.