0
votes

I am having a problem with the routing. Navigation is successful to a childview like oRouter.navTo('child') .... but when the application goes to the child's controller ... well I cannot access child's View in the init function.

debugging

this.getView() fails as it does not have it ...perhaps loaded yet? in the console when I debug it does not exist... Uncaught TypeError: this.getView is not a function

but if I use console.log to write it I can see that it's there?!:

with console

I don't understand what's happening here as I also have another navigation to another, different view and it works there perfectly.

I tried setting the routing to async: false but it did not help...

Manifest snippet:

    "routing": {
        "config": {
            "routerClass": "sap.m.routing.Router",
            "viewType": "XML",
            "async": false,
            "viewPath": "namespace.view",
            "controlAggregation": "pages",
            "controlId": "myApp",
            "clearControlAggregation": false
"routes": [{
            "name": "TargetMain",
            "pattern": "",
            "target": "TargetMain"
        }, {
            "name": "toEmpty",
            "pattern": "child1",
            "target": "toEmpty"
        }, {
            "name": "child2",
            "pattern": "child2",
            "target": "child2"
        }, {
            "name": "toError",
            "pattern": "child3",
            "target": "child3"
        }, 
        }],
"targets": {
            "TargetMain": {
                "viewType": "XML",
                "transition": "page",
                "clearControlAggregation": false,
                "viewName": "Main",
                "viewLevel": 1
            },
2

2 Answers

0
votes

Who is calling the init method? You can use the onInit method ..its called once the view elements are initialized

0
votes

When you call navTo on the router, it will

  1. match the patterns according to your config
  2. find corresponding target (may be more than one)
  3. in each target, a view is listed
  4. if that view is not loaded yet, it will load the view
  5. Duting loading of the view, the view also loads its own controller (controller is specified in the view)
  6. During initialisation, onInit in the controller is called
  7. after inisialisation, the event "route matched" is fired

Two things to be observed: the controller is loaded by the view, so this.getView() is always available.

what you see in the screenshot of the debugger, may well be a Component, rather than a Controller. You can find out by calling this.getMetadata(). A controller has no private member oModels. A component has such a private member. A component has an init method, which is called when the component is initialised. A controller has an onInit method.

Now why a component is loaded, I can't tell from screenshts. Maybe one of your views that are loaded by the router contains a ComponentContainer which, in turn, loads a Component and calls the ìnit method of the component.

The component has no view, and no getView method.