在UI5中编辑/更新绑定的数据时未发生数据绑定

时间:2019-03-07 05:56:02

标签: javascript model-view-controller model sapui5

我有一个包含克隆按钮的主从页面。单击克隆按钮后,第一个列表项的数据被放入模型(detailModel-在component.js中声明),并打开一个新屏幕。该列表项的数据绑定到某些输入字段。当该输入字段中的任何一个被更新/擦除后,用户又导航而不保存它并再次出现在该页面上时,该更新/擦除的字段将保持空白。我希望这些数据进入较早出现的那个字段,但现在为空白。我模型中的数据也没有改变。下面是代码:

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);
            }



        });
    });

1 个答案:

答案 0 :(得分:0)

如果可以更改为oDataModel,则可以使用resetChanges方法。

如果您做不到,则必须手动进行一些操作。一旦弹出新窗口,将保存数据,用户将更改,如果取消,则将保存的数据恢复到jsonDataModel上。