on 07-02-2020 12:30 AM
Hi,
I have the following code inside a controller that is reading a record using oData model. In success function I am trying to access the outer function variable "that". But I get the error "Uncaught ReferenceError: that is not defined". I am not able to identify the issue. Can you please advise what I am missing? Thanks for the help
Regards, Ravikanth
onRouteMatched: function () {
debugger;
var oModel = this.getOwnerComponent().getModel("PES");
var path = "/DEFAULTSSet('ABCD')";
var that = this;
oModel.read(path, {
async: false,
success: function (oData) {
that.getOwnerComponent().getModel("defaults");
},
error: function (oError) {
}
});
},
Hi Ravikanth,
Try binding your method with this:
oModel.read(path,{
async: false,
success:function(oData){
this.getOwnerComponent().getModel("defaults");
}.bind(this),
error:function(oError){}});
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ravikanth,
I believe most of the variable in the outer function should still be available in your success callback. What is meant is oRouteMatched function variables should also be available except the arguments if used.
For arguments, you can still save this argument in the outer function as local variable and you should be able to access the local variable in the inner function.
Thanks
Prashil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Maybe it's because of the onRouteMatched.
When passing the function onRouteMatched, pass like below onRouteMatched.bind(this) or pass 'this' explicitly to function as show below
https://help.sap.com/doc/saphelp_uiaddon20/2.05/en-US/f9/6d2522a5ca4382a274ae3c6d002ca0/frameset.htm
In the same way, u don't need to pass this to that, you can do like below
success: function(){
}.bind(this)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mahesh,
Thanks for the input. My onRouteMatched function is called like this:
oPrepareRouter.attachPatternMatched(this.onRouteMatched, this);
As you suggested, I could achieve the access to "this" variable using .bind(this) but as per one of the comment in below blog accessing "this" using closure is faster than binding this onto the method. Hence I am trying to achieve the access using closure.
https://blogs.sap.com/2016/01/26/pattern-to-avoid-var-that-this-with-odata-model-callback-functions/
Regards,
Ravikanth
asdfsdafgdafgdasdf I am pretty sure passing "this" by bind will not cause any noticeable performance issues.
Coming to your "that" issue, I hope the place you are calling the below code has the proper reference in "this"
var that = this;
oPrepareRouter.attachPatternMatched(this.onRouteMatched, this);
and make sure belore calling the below that "this" has the view reference
oModel.read(path, {
then surely inside the successhandler "that" will be accessible
Hi Mahesh,
Thanks again for looking into it. At the time of assigning "var that = this" and before "oModel.read" call, this variable has the view reference. Infact oModel itself is created using this variable ("this.getOwnerComponent().getModel("PES");".)
As of now I am going ahead with .bind(this) variant.
Thanks
Ravikanth
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.