简体   繁体   中英

Data binding not happening when the binded data is edited/updated in UI5

I have a master-detail page which has a clone button. On clicking the clone button, the data of the first list item is put into a model (detailModel - which is declared in component.js) and a new screen is opened. The data of that list item is binded to some input fields. When any of that input field is updated/erased and then user navigates back without saving it and comes again on that page, that updated/erased field stays blank. I want that data to come in that field which was coming earlier but it is coming blank. Data in my model is also not changing. Below is the code :

Component.js :

this.detailModel = new sap.ui.model.json.JSONModel();
        this.detailModel.setDefaultBindingMode(sap.ui.model.BindingMode.OneWay);
            sap.ui.getCore().setModel(this.detailModel, "detailModel");

Controller.js :

        /*eslint-disable no-console, no-alert */
    sap.ui.define([
        "sap/ui/core/mvc/Controller",
        "sap/ui/model/json/JSONModel",
        "hmel/TravelandGuestHouse/controller/formatter",
        "sap/m/MessageToast",
        "sap/m/MessageBox",
        "sap/m/Button",
        "sap/m/Dialog",
        "sap/m/Text",
        'sap/m/Label',
        'sap/m/TextArea'
    ], function (Controller, JSONModel, formatter, MessageToast, MessageBox, Button, Dialog, Text, Label, TextArea) {
        "use strict";
        return Controller.extend("hmel.TravelandGuestHouse.controller.CloneTravelRequest", {

            onInit: function () {
                this.router = sap.ui.core.UIComponent.getRouterFor(this);
                //for cloning request
                this.detailModel = sap.ui.getCore().getModel("detailModel");
                this.getView().setModel(this.detailModel, "detailModel");
                //for sending data to guest house screen
                this.getView().byId("date").setDateValue(new Date());
                this.router.attachRoutePatternMatched(this._handleRouteMatched, this);
            },

            _handleRouteMatched: function (evt) {
                if (evt.getParameter("name") !== "CloneTravelRequest") {
                    return;
                }

                this.getView().invalidate();

                    var that = this;

                //fetching Train Names
                $.ajax({
                    url: "/Non_sap_create_requests/odata/TravelPrpTrainDetails",
                    method: "GET",
                    dataType: "json",
                    success: function (data) {
                        that.getView().getModel("Model").setProperty("/TravelPrpTrainDetails", data.value);
                        var tempModel = that.getView().getModel("detailModel");
                        var train = tempModel.getData().TrainName;
                        var trainKey = formatter.pickTrainKeyFromModel(data.value, train);
                        that.getView().byId("trainName").setSelectedKey(trainKey);
                        that.setLocations();
                        var nameofPass = tempModel.getData().NameOfPsngr;
                        if (nameofPass === "" || nameofPass === null) {
                            that.getView().byId("nameOfP").setValue(tempModel.getData().Requestee);
                        }
                    },
                    error: function (err) {
                        console.log(err.message);

                    }
                });

                var fdate = new Date();
                this.getView().byId("date").setDateValue(new Date());
                this.getView().byId("date").setMinDate(fdate);
            }



        });
    });

If you can change to an oDataModel you can use the resetChanges method.

If you cant, you `ll have to do it a little bit more manually. Once the new windows pops up save the data the user will change and if he cancels it restore the saved data on the jsonDataModel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM