Drafts
First to enable Draft handling you have to enable drafts for your entity.
service Service {
@odata.draft.enabled
entity Entity as
projection on db.Entity;
}What is draft handling?
A draft is a temporary copy of an object, indicated by the IsActiveEntity column which is part of the key. An object can also have a draft, indicated by the HasDraftEntity column.
Entities with draft functionality cannot be created nor edited directly. These functions are performed on the drafts and are subsequently applied to the object.
When you "create", only a draft is created and not an actual object. In order to create the object, you have to 'activate' the draft.
To edit an object, a draft must first be created. The draft can then be edited before being activated.
Read
If you do a normal binding as you always would, then only objects are shown and not drafts.
To also see Drafts you have to write a filter to do so.
this.byId("Table").bindItems({
path: "/Objects",
template: this.byId("template"),
filters: new Filter("IsActiveEntity", FilterOperator.EQ, false),
});Alternatively, you can view both objects without drafts and drafts simultaneously like this.
this.byId("Table").bindItems({
path: "/Objects",
template: this.byId("template"),
filters: new Filter({
filters: [
new Filter(
"SiblingEntity/IsActiveEntity",
FilterOperator.EQ,
null
),
new Filter("IsActiveEntity", FilterOperator.EQ, false),
],
and: true,
}),
});Create a new draft
const pNewObject = this.getModel().bindList("/Objects").create();
this.getView().setBusy(true);
const oNewObject = await result.created();
this.getView().setBusy(false);
oNewObject.getPath(); The getPath() function can be used to bind the element to whatever you want.
Edit an existing object
To edit an existing object, it is necessary to create a draft of it. However, since objects cannot have multiple drafts simultaneously, it is recommended to check for existing drafts first.
if (context.getProperty("HasDraftEntity")) return;To create a draft from an object, you need to write a normal context binding (element binding) and then call the CAP created. Be careful not to forget that IsActiveEntity is part of the key.
this.getOwnerComponent()
.getModel()
.bindContext(`${context.getPath()}/drafts.draftEdit(...)`, context)
.execute();Activate
To activate a draft, you need to write a normal context binding (element binding) and then call the CAP created. Be careful not to forget that IsActiveEntity is part of the key.
this.getOwnerComponent()
.getModel()
.bindContext(`${context.getPath()}/drafts.draftActivate(...)`, context)
.execute();