Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Is My Main Controller Being Called Twice?

Tags:

sapui5

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"
    }
  }
}
like image 232
Adam Harkus Avatar asked Nov 23 '17 14:11

Adam Harkus


People also ask

Can two Controllers have same name in one controller?

To answer your specific question, you cannot have two methods with the same name and the same arguments in a single class; using the HttpGet and HttpPost attributes doesn't distinguish the methods. Save this answer. Show activity on this post. Save this answer.

How does MVC know which controller to use?

This is because of conventions. The default convention is /{controller}/{action} , where the action is optional and defaults to Index . So when you request /Scott , MVC's routing will go and look for a controller named ScottController , all because of conventions.

Can a controller call another controller?

Yes, you can call a method of another controller. The controller is also a simple class. Only things are that its inheriting Controller Class. You can create an object of the controller, but it will not work for Routing if you want to redirect to another page.

What is default controller in MVC?

The default MVC mapping is /[Controller]/[ActionName]/[Parameters] . For this URL, the controller is HelloWorld and Welcome is the action method.


1 Answers

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.

like image 159
Boghyon Hoffmann Avatar answered Sep 28 '22 00:09

Boghyon Hoffmann