How to make an asynchronous OData .create() request?

11,866

Solution 1

The SAPUI5 ODataModel changed some time ago and now you have to use an absolute path with the ODataModel eg '/myDataSet' not 'myDataset' as you keep showing. I think this maybe the root of your problems and why your call is erroring before showing the progress indicator.

As @Qualiture highlights the OData calls are async by default, you dont need to change any paramater. Interesting that you are on version 1.19.1 and the OpenUI5 snapshot available to the rest of us is currently 1.16.8, regardless of versions it should be backwards compatible so you dont need to change. Should be!!

Why has it been added. My guess, most of the times you will want to use an asynchronous call that is why it is the default setting, there are some use cases where you might want to do a sync call with a POST for example calling an Entities function, or sending an update onBeforeUnload, places you cannot wait for a callback, in these scenarios I chose to wrap the call(s) in a BATCH operation which does have a boolean async parameter, would be easy if it was a available on the Create Method.

A couple more things you may not know have been added, you may consider using oDModel.createEntry(sPath,property) with oDModel.submitChanges(...) instead of oDModel.create(...) this is a relatively new feature, it verifies your data against the Entities metadata before sending and also creates a bindable context for you.

Also you may want to use the Models events for controling the progress animation as you can set these up once for all operations, instead of relying on the individual callbacks

oDModel.attachRequestSent(function (oEvent) {
    myAnimationIndicator.stop();
});

oDModel.attachRequestCompleted(function (oEvent) {
    myAnimationIndicator.stop();
});

oDModel.attachRequestFailed(function (oEvent) {
    myAnimationIndicator.stop();
});

see JSBIN: RowRepeater with Stackoverflow data for an example

Solution 2

Would the ODataModel .read(sPath, oContext?, oUrlParams?, bAsync?, fnSuccess?, fnError?) method suffice?

Contrary to the .create() method, it performs a GET request as opposed to a POST, but it allows for asynchronous requests (default is asynchronous)

Share:
11,866
Dr. Dong Zhu
Author by

Dr. Dong Zhu

Dr. Zhu has been working as a software specialist (>20 years) with extensive experiences and knowledge of variant software technologies: ● SAP SAPUI5 & Fiori, Personas, ABAP. ● HTML5/CSS/JS/OData ● Javascript,C/C++/Java/C#/.Net/Linux/Python ● Cordova/iOS/Android Dong's blogs: "SAPUI5 Programming Tips & Tricks": http://prog-tips.blogspot.dk/ "SAP Backend Screen Personas Tricks & Tips" http://dzpersos.blogspot.dk/

Updated on June 04, 2022

Comments

  • Dr. Dong Zhu
    Dr. Dong Zhu almost 2 years

    In my SAPUI5 app, I have an animation made by progress indicator which should be running while waiting for the response of an OData model .create() service (because it takes a while to get the response back to the UI).

    The issue is that the animation stops after the .create() request is sent.

    Does anyone know how to make an async .create() OData request?

    My code looks like this:

    oODModel = new sap.ui.model.odata.ODataModel(myUrl);
    oEntry = myNewDataEntry;
    myAnimationIndicator.start();         //animation starts to run
    var onSuccess = function(){myAnimationIndicator.stop();};
    var onError = function(){myAnimationIndicator.stop();};
    oODModel.create('myDataSet',oEntry, null, onSuccess, onError);  //From here the animation stops
    
  • Dr. Dong Zhu
    Dr. Dong Zhu about 10 years
    Hi Jasper_07, FYI, myUrl is like ".._SRV/", so when it is added with "myDataSet" string, the path is ".._SRV/myDataSet". I saw in other case where the root url is ".._SRV", then the path should be added like "/myDataSet". Otherwise, the service would not work. It is a good idea to try other methods as you mentioned. Thanks.