2

I have a main screen with a tile the user can press to go to another page. The onInit for this second page works fine in getting/setting the model and the data shows correctly.

If I 'go back' to the first page (after I have made changes on the second screen), and then click the tile to go to the second page, it doesn't call the onInit this second time and so the data reflects the changes that were made and not what I want (the true initialized data). I tried changing the onInit to onBeforeRedendering hoping that it would re-initialize the model/data but it doesn't seem to reset everything correctly.

Is there a way on going back to do something to force the onInit to be called the next time the page is called? I think, if I can make it so the onInit is called each time the page is called, that it would fix my problem.

Here is the portion of my controller for the onInit and 'go back'....

sap.ui.define([
  'sap/ui/core/mvc/Controller',
  'sap/ui/model/json/JSONModel',
  'sap/viz/ui5/controls/common/feeds/FeedItem',
  'sap/m/MessageBox',
  'sap/viz/ui5/data/FlattenedDataset'
], function(Controller, JSONModel, FeedItem, MessageBox, FlattenedDataset) {
  "use strict";

  var ColumnController = Controller.extend("controllers.Quarter", {
    onInit: function(oEvent) {
      var oRouter = sap.ui.core.routing.Router.getRouter("router");
      var myView = this.getView();
      var today = new Date();
      var year = today.getFullYear();
      var yr = year.toString();
      var mnth = today.getMonth();
      var qtr = Math.floor((mnth / 3));
      this.makeYearList(yr);
      var mthis = this;
      var oModel = new sap.ui.model.json.JSONModel();
      oModel.setData({
        yr: yr
      });
      sap.ui.getCore().setModel(oModel);
      myView.byId("mySelectMenu").setSelectedKey(yr);
      myView.byId("mySelectMenu").attachChange(function() {
        yr = this.getSelectedKey();
        mthis.checkYr(yr, qtr);
        mthis.recList(myView, yr, qtr);
      });
      myView.byId("selQtr").attachChange(function() {
        qtr = this.getSelectedKey();
        mthis.checkYr(yr, qtr);
        mthis.recList(myView, yr, qtr);
      });
      oRouter.attachRouteMatched(function(oEvent) {
        mthis.checkYr(yr, qtr);
        mthis.recList(myView, yr, qtr);
      });
    },

    goBack: function() {
      var oHistory = sap.ui.core.routing.History.getInstance();
      var sPreviousHash = oHistory.getPreviousHash();
      var oView = this.getView();
      if (sPreviousHash) {
        window.location.replace("#/" + sPreviousHash);
      } else {
        window.location.replace("#");
      }
    },

  });

  return ColumnController;
});

I'd appreciate any advice.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
user3861284
  • 241
  • 2
  • 10
  • 22
  • Does this answer your question? ["onBeforeRendering" or "onAfterRendering" is not called every time the view is opened](https://stackoverflow.com/questions/55082731/onbeforerendering-or-onafterrendering-is-not-called-every-time-the-view-is-o) – Boghyon Hoffmann May 17 '21 at 17:10

2 Answers2

2

Put the logic to reset the model data into the route matched handler.

matbtt
  • 4,230
  • 19
  • 26
-1

The better way to overcome this problem is, use shell for your page one and two. Shell will automatically destroy your content(if you are in page two then page one content would be destroyed and vice versa). else, you need to destroy the content manually to overcome duplicate id issue, u need to destroy by your own and call the controller wherever you want.

jag789654123
  • 177
  • 2
  • 6
  • 15
  • I do have a shell on both pages, so from what you said it sounds as though the shell is not destroying the content. Unless I need to set an option on the shell.... – user3861284 May 21 '15 at 13:03