openui5 icon indicating copy to clipboard operation
openui5 copied to clipboard

1.28.2 sap.ui.model.odata.v2.ODataModel createEntry(...)

Open hschaefer123 opened this issue 11 years ago • 4 comments

I am currently trying to implement our MasterData CRUD Template for our OData services on HANA and PHP.

Besides my issues with setting batchMode = false and not able to handle success/error on using submitChanges() i ran into problems creating new entries.

Using var oEntry = this.getView().getModel().createEntry(sCreatePath)

and using it via oView.bindElement(sObjectPath); // something like "/UmeUsers('id-1427727787066-30')"

There is no valid Context inside my View! Also the oDataModell is doing a GET request on the creatingPath /UmeUsers('id-1427727787066-30')

The .createEntry return an context and the entry is stored inside model, but it seems that the entry is not able to be bound using oView.bindElement using the binding Path.

that seems to be invalid for me, because i try to create a new entry?!? Since 1.28.2 i use the default settings for defered batchGroup="changes" so editing existing entries works for me and i can use submitChanged() to update.

There is also a bug inside the createEntry line // remove starting / for key only sKey = sPath.substring(1) + "('" + jQuery.sap.uid() + "')";

because this works only for KEYS of type String!!! What about keys of type integer, which needs an uri like /UmeUsers(6)

If the UID will only be used internally as a phantomId for newly generated entries and filtered out on submitChanges(), this would be ok, but how can i disable the GET request on non existing data?

Do you have a small code snippet for me, how to be able to do CRUD operations using the new v2/OData model.

Currently the SAPI5 diagnostics Controll tree will not show binding infos for me, even if the binding is valid?!?

Any help would be greatly appreciated ;-)

Cheers Holger

hschaefer123 avatar Mar 30 '15 15:03 hschaefer123

Hi Holger,

thanks for reporting! I`ll forward this to the responsible OData expert to verify and will get back to you asap.

Best! Thilo

ghost avatar Apr 01 '15 07:04 ghost

Hi Holger,

bindElement always triggers a request if the referenced entity does not exists in the models cache. So it could work only for entities that exists at the backend.

createEntry returns a context object that can be set directly to the control tree via setBindingContext. It creates a transient entity object in the ODataModels cache. The context points to this object. To create the entity at the backend you have to call submitChanges. If the creation was successful the context will be updated by the model so it points to the newly created entity.

The ID we create for the transient object is for binding purposes only and is absolutely correct. The real ID has to be created by the backend.

var oContext = this.getView().getModel().createEntry(sCreatePath); this.getView().setBindingContext(oContext);

using CRUD methods should be easy:

oModel.read("/pathToMyEntity",{success: mySuccessHandler, error: myErrorHandler});

oModel.delete("/pathToMyEntity",{success: mySuccessHandler, error: myErrorHandler});

oModel.update("/pathToMyEntity", myEntityData, {success: mySuccessHandler, error: myErrorHandler});

oModel.create("/pathToMyEntitySet",myEntityToCreate, {success: mySuccessHandler, error: myErrorHandler});

Have a look at the API doc for the complete parameter set these methods provide.

BR, Jan

loginger avatar Apr 01 '15 08:04 loginger

Hi Jan, thanks for the help.

I figured out after some test, that my problem consists of initially using oView.bindElement(...) (onMasterLoaded) and then try to set the createEntry via setBindingContext().

I fixed it using oView.unbindElement() before setBindingContext

var oView = this.getView(), oContext = oView.getModel().createEntry(sCreatePath); oView.unbindElement(); oView.setBindingContext(oContext);

Now it works as expected ;-)

Is this the right way of doing it?

Cheers Holger

hschaefer123 avatar Apr 02 '15 10:04 hschaefer123

Hi Holger,

the bindElements context is the leading one on a control/view. If you set a context via setBindingContext to a control that already has an ElementBinding it does only have an effect if the ElementBinding is relative. The newly set context will become the parent context for this relative ElementBinding.

I think in your case the ElementBinding is absolute, so the setBindingContext has no effect until you unbind it. So your approach is fine.

BR, Jan

loginger avatar Apr 02 '15 10:04 loginger