javascript - SAP Open UI5 - Call function inside attachRequestCompleted -


i have question attachrequestcompleted in sap open ui5.

my code looks this:

test : function (oevent) {                 model = new sap.ui.model.json.jsonmodel();                 // load json in model                 model.loaddata("http://localhost:8080/getjson");                  model.attachrequestcompleted( function(){                     console.log(model.getdata());                     this.makesomething()                 });         }, 

i want call function makesomething after model loaded it's not possible.

i tried call after function this. function gets called model isn't loaded.

test : function (oevent) {                 model = new sap.ui.model.json.jsonmodel();                 // load json in model                 model.loaddata("http://localhost:8080/getjson");                  model.attachrequestcompleted( function(){                     console.log(model.getdata());                 }, this.checkelement());         }, 

is possible?

the this keyword in javascript tricky. w3schools states here:

in javascript, thing called this, object "owns" javascript code.

the value of this, when used in function, object "owns" function.

the value of this, when used in object, object itself.

the keyword in object constructor not have value. substitute new object.

the value of become new object when constructor used create object.

in case, if call this inside test method, this refer current controller. can use this inside method access other methods of controller.

however, if use this inside callback method, this - owner of code - no longer controller. callback method. this.makesomething() not exist.

the common way around create variable called that, give value of this while this has value want access later on. can access callback method; in callback method, that variable not have changed, whereas this different.

a code sample worth thousand words. see changes below.

test : function (oevent) {     var = this;     model = new sap.ui.model.json.jsonmodel();     // load json in model     model.loaddata("http://localhost:8080/getjson");      model.attachrequestcompleted( function(){         console.log(model.getdata());         that.makesomething();     }); }, 

when using ui5, create variable @ same level controller methods called _globalthis. in oninit method, assign value of this , can access same variable every 1 of callback methods.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -