cancel
Showing results for 
Search instead for 
Did you mean: 

accessing Controller inside OData Read success Function

vengadesh_r
Explorer
0 Kudos

Hi All,

I am trying to access controller object inside OData Read Success function.

I am trying to use var that = this; But somehow this is not working.

Can anyone help me to resolve this. my UI5 library version is 1.38.1.

that-this.png

Thanks,
Vengadesh R

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor

u need to access it first inside, then only in the debugger you can access "that". But you don't need to do that old approach, try doing like below, yyou can access "this" directly in the success handler.

model.read("...",
success:{
...
}.bind(this) // This will pass the "this" reference of the called function inside the success handler
)

Mahesh

vengadesh_r
Explorer
0 Kudos

@maheshkumar.palavalli

It worked like charm.. Thanks for your response. Earlier I was using the bind wrongly.

Now I got next issue.

Now I need to use the same this in attachBatchRequestCompleted function. Can you please throw some light ?

maheshpalavalli
Active Contributor
0 Kudos

vengadesh.r It's the same anywhere..

https://ui5.sap.com/#/api/sap.ui.model.odata.v2.ODataModel%23methods/attachBatchRequestCompleted

attachBatchRequestCompleted(... , function(){}.bind(this))

but as per the documentation, they are passing oListner, which means the context object to the event handler. So maybe try like below as well.

attachBatchRequestCompleted(... , function(){}, this)

Regards,

Mahesh

vengadesh_r
Explorer
0 Kudos

Thanks Mahesh. It saved my Day

boghyon
Product and Topic Expert
Product and Topic Expert

vengadesh.r There is a gotcha about passing an anonymous function directly: it cannot be detached. Better pass a function reference and the listener.

attachBatchRequestCompleted(this.onBatchRequestCompleted, this);

This will make sure that the handler can be also detached once it's no longer required. See https://stackoverflow.com/a/62120803/5846045.

Thank you Mahesh , You are a Champ !!

Answers (1)

Answers (1)

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

Same issue as https://answers.sap.com/questions/13194201/. In the debugger environment, closure variables cannot be accessed within a function due to how the V8 (JS engine in Chromium) optimizes JS in the runtime.