user3861284
user3861284

Reputation: 249

How to init/Re-Init Page or trigger onBeforeRendering again?

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.

Upvotes: 2

Views: 2860

Answers (2)

matbtt
matbtt

Reputation: 4231

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

Upvotes: 2

Jag
Jag

Reputation: 169

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.

Upvotes: -1

Related Questions