Search code examples
javascriptodatasapui5odata-v4

How to get OData Context from parent Context in UI5


I'm using the OData V4 model in UI5. I've created a binding with some expands in it and now I try to obtain the context of a child entity.

Here is the code how I bind entities to some element. As a result I get an object with on 'SomeEntity' and an array with 'SomeOtherEntity' as a property.

oPage.bindElement({
    path: /SomeEntity(id),
    parameters: {
        $expand: {
            SomeOtherEntity: {
                $select: ['ID', 'name', 'sequence'],
                $orderby: 'sequence'
            }
        }
    }
});

Now I can get the context of the binding with oPage.getBindingContext() and can execute methods like requestObject, setProperty, create and delete from this object.

What I want is to obtain the context of one of the 'SomeOtherEntity' properties to (for example) delete one of them. I have no idea how to accomplish this. Anybody has an idea?


Solution

  • You can create an own ListBinding to SomeOtherEntity and filter the desired set.

    (I'm not quite sure, but it might be necessary to trigger a refresh on the ListBinding to force an initial load)

    After the data is loaded (dataReceived-Event), delete all the Contexts. Each Delete returns a Promise and you can proceed with a Promise.all.

    var oDataModel = this.getModel();
    var aPromises= [];
    var oListBinding = oDataModel.bindList("/SomeOtherEntity", undefined, undefined, new Filter("ID", FilterOperator.EQ, sIdToDelete), {
            $$operationMode: OperationMode.Server
        });
    
    oListBinding.attachEventOnce("dataReceived", function (oEvent) {
        var aContexts = oListBinding.getContexts();
        aContexts.forEach(function (oContext) {
            aPromises.push(oContext.delete("$auto"));
        });
        Promise.all(aPromises).then(function () {
            /* Cleanup after Deletion
        });
    });