Reputation: 179
I have two functions in the controller, one of which is used for processing some event, let's say button click.
This one works fine:
handleUploadPress: function(oEvent) {
var oFileUploader = this.byId("streamerUploader");
if (!oFileUploader.getValue()) {
MessageToast.show("Choose file");
return;
}
And this one throws error:
showResponse: function(id, response) {
this.byId("streamerUploader").someMethod();
}
In both functions "this" equals event provider, however, this.byId() returns undefined in the second one. What is the reason?
Upvotes: 0
Views: 3015
Reputation: 2424
this does not point to the controller when called from a callback method.
Your handleUploadPress() method, presumably, is an event handler and handles the press of a file upload button. The owner of this method is the controller, and calling this.byId(...) will behave as expected - it will return the component in the view with the specified ID.
However, in your callback method, this is not the controller. The method this.byId is undefined - it's a method defined by SAP UI5 controllers.
What you need to do
Before you send your request with req.then(...), declare a variable whose value will be the this you will need to access, as such:
var that = this;
Then, to use the byId method, go by this variable.
that.byId(...).doSomeMethod();
I advise you read this question and answers to understand more about this in Javascript.
Upvotes: 1