1

I would like to add new entity using form and save it after filling all required data. Because this objects are complex I don't want to fill data directly on list.

Only way that I found to create new entity when using OData V4 is by use of method Create on sap.ui.model.odata.v4.ODataListBinding. example from openUI5 documentation

Unfortunately I'm not able to bind to this created entity from other View or even Form on the same view.

Maybe this is wrong approach. Does anybody know any solution/example of creating new entity without aggregation binding with OData V4?

0

1 Answer 1

0

My solution for this issue was to create list biding in controller like this:

var oItemTemplate = new sap.m.ColumnListItem();
this._oBindList = new sap.m.List({
            items: {
                path: "/somePath",
                parameters: {
                    $$operationMode: "Server",
                    $$updateGroupId: "SOME_GROUP"
                },
                length: 1,
                template: oItemTemplate
            }
        });
this.getView().addDependent(this._oBindList);

Then I bind JSONModel with appropriate structure to View. When user click on Add I just call Create method on this list binding with data from JSONModel

var newData = this.getView().getModel("ModelName").getData();
var oJsonData = JSON.parse(newData);
var oBinding = this._oBindList.getBinding("items");

oBinding.create(oJsonData);
5
  • OData V4 does not have getData() method on Model. Did you switch back to V2? Thanks for your response
    – Gana
    Aug 7, 2019 at 12:28
  • I probably poorly explained this. In this line: var newData = this.getView().getModel("ModelName").getData(); I get JSONModel that is binded to my View. I keep it usualy in folder model as JSON file. When user is opening view for adding new entity I just bind this declared model to View. This way I have model that is binded to my View inputs etc.. and I can simply use getData on that model to get input to create method Aug 7, 2019 at 13:50
  • I am getting an error: Cannot read property 'create' of undefined
    – zygimantus
    Feb 2, 2020 at 23:01
  • Hi @zygimantus, this looks like this_oBindList isn't initiated. I run this part of code (first described part of code) in init function of my controller. And the second part is placed in onAddPress event handler for example. If you will still have a problem please paste your code and I will take a look. Feb 3, 2020 at 12:17
  • I moved this code from onInit into onBeforeRendering and it worked.
    – zygimantus
    Feb 3, 2020 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.