Skip to Content
0

Pass custom parameter to oModel.update

Oct 18, 2016 at 01:44 PM

110

avatar image
Former Member

Hey,

I update my oModel in a for loop. In the success callback of the oModel update function I need a specific parameter. How can I pass a specific parameter to this callback function? When I use it like in the code snippet below, the varaible "i" has not the right value because success callback is called anytime:

for (var i = 0; i < ...; i++)
   oModel.update("/" + objectKey, objectFromStorage, {
      success : function() {
         console.log(i);
      },
      error : function() {
         console.log("Error when updating object: " + objectKey)
      }
   });
}

Thanks for your help and best regards,

David

10 |10000 characters needed characters left characters exceeded

why?

/////////////////

0
Former Member
Jun Wu

Thanks for this really helpful answer...

0

why you need that one? what's ur real requriement?

0

Hi David- use this.object = "YOUR VALUR", you can access this object in success call back function.

This is my personal suggestion to you, please dont use ODATA model in for loop. It create multiple calls and slow down the application performance. ex: Use batch call instead of for-loop

Regards,

Karthik A

0
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Jamie Cawley
Oct 18, 2016 at 01:57 PM
0

You should probably try to convert this to use a batch call, but to answer your question you may be able to do something like

for(var i=0;i<...;i++)
   oModel.update("/" + objectKey, objectFromStorage, {
myVar: i,
      success :function(){
         console.log(this.myVar);},
      error :function(){
         console.log("Error when updating object: " + objectKey)}});}

Regards,

Jamie

SAP - Technology RIG

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

I've tried your answer, but I get "undefined" for this.myVar

Best regards,

David

0
Akhilesh Upadhyay Oct 18, 2016 at 03:08 PM
0

I am not sure but you can try as below ways:

//you can handle callback function's arguments like oData and/or oResponse

success : function(oResponse) {

//you can get info about updated object in oResponse (may help)

}

Or

for (var i = 0; i < ...; i++)
    oModel.update("/" + objectKey, objectFromStorage, {
        this.updatedIndex: i,
        success: jQuery.proxy(this.onSuccess, this),
        error: function() {
            console.log("Error when updating object: " + objectKey);
        }
    });
}
onSuccess: function() {
    console.log(this.updatedIndex);
}

Regards,

Akhilesh

Share
10 |10000 characters needed characters left characters exceeded
Ryan Crosby Oct 18, 2016 at 05:54 PM
0

Hi David,

The asynchronous callback won't have any reference to the values of i or objectKey when they execute. You can try using bind which will be accessible from "this". See below for an example:

for (var i = 0; i < ...; i++)
   oModel.update("/" + objectKey, objectFromStorage, {
      success : function() {
         console.log(this);
      }.bind(i),
      error : function() {
         console.log("Error when updating object: " + this)
      }.bind(objectKey)
   });
}

Regards,

Ryan Crosby

Share
10 |10000 characters needed characters left characters exceeded