Skip to Content
avatar image
Former Member

Pass custom parameter to oModel.update

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

Add comment
10|10000 characters needed characters exceeded

  • Former Member Jun Wu

    Thanks for this really helpful answer...

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

  • 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

  • Get RSS Feed

3 Answers

  • Oct 18, 2016 at 01:57 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 18, 2016 at 03:08 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 18, 2016 at 05:54 PM

    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

    Add comment
    10|10000 characters needed characters exceeded