Search code examples
javascriptsapui5

How can I check on already created entries in my model?


I am quite new to sapui5 and can't solve the following problem.

I have a SplitApp. When I chose the first entry in my master view, a detail view is called. In the onInit event from the detail view the method createEntry runs. To process my input data I call submitChanges.

Now when I call the second entry on my master view, another view is called and for that second view the onInit event runs and creates an own entry. But the created entry from the first view from the method createEntry will not be deleted. Now I call the first entry on my master view and again the onInit event runs and a second ID for that first view will be created. This leads to a mistake when I run the submitChanges method. The more often I switch between the views, the more entries I create.

So is there an event I can use to call deleteCreatedEntry? I already tried onExit and onBeforeHide. Or is it better to check, if an entry is already created? Which code do I have to use for this?

this.oContext = this.getModel().createEntry("/MyoData01Set", {
                success: this._successSave.bind(this),
                error: this._errorSave.bind(this)
            });

Many thanks and best regards Julia

In addition 13.05.20:

I implemented the suggestion, but still it is not working. I get an error, when I try to enter the method _this.bindNewContext(); Please take a look at the code:

handleMatchedDetail: function (oEvent) {
            this.getModel().metadataLoaded().then(function () {
                this.getModel().resetChanges().then(function () {
                    this._bindNewContext();
                });
            }.bind(this));
        },

        _bindNewContext: function () {
            this.oContext = this.getModel().createEntry("/Kardex01Set", {
                success: this._successSave.bind(this),
                error: this._errorSave.bind(this)
            });
            // }
            var oBindingPath = {
                path: this.oContext.getPath(),
                events: {
                    change: function () {
                        this.getModel().read("/Kardex01Set('0')", {
                            success: function (oData, oResponse) {
                                this.getView().getModel().setProperty(this.oContext.getPath() + "/tanum", oData.tanum);
                            }.bind(this)
                        });
                    }.bind(this)
                }
            };
            this.getView().bindObject(oBindingPath);
        },

Solution

  • You can simply call resetChanges on your model. This will delete all changes that have not been submitted yet.

    See the API description for details.