6

All other routings are fine, but for some reason the main controller is being called twice. Why would this happen?

onInit: function() {
  var oRouter = this.getOwnerComponent().getRouter();
  oRouter.getRoute("main").attachMatched(this._onRouteMatched, this);
  this.getView().setModel(new JSONModel({
    Jobs: []
  }), "job");
},

Is this down to the routing config?

"rootView": {
  "viewName": "CompleteSurvey.view.Main",
  "type": "XML"
},
"routing": {
  "routes": [{
    "name": "main",
    "pattern": "",
    "target": ["main"]
  }],
  "config": {
    "routerClass": "sap.m.routing.Router",
    "viewType": "XML",
    "viewPath": "CompleteSurvey.view",
    "controlId": "app",
    "controlAggregation": "pages"
  },
  "targets": {
    "main": {
      "viewName": "Main"
    }
  }
}
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Adam Harkus
  • 2,050
  • 2
  • 36
  • 64

1 Answers1

7

The reason why your Main controller is created twice is because its view is created twice.

  1. Your Component fetches manifest.json and looks at the rootView to create the assigned view ("CompleteSurvey.view.Main").
  2. Your router gets initialized, sees that the current hash / pattern is "", and creates the corresponding view which is the "Main" view again.

The current best practice is to have a separate root view. You can keep the Main for the "" pattern, but avoid using the same view again as a root view.


For further references, take a look at the tutorial Navigation and Routing and this answer.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170