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

advertisements

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


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