Nali
Nali

Reputation: 157

Navigate to other controller form app controller

So I have my app controller as always in sapui5:

sap.ui.define(["sap/ui/core/mvc/Controller"], function(Controller) {
"use strict";
  return Controller.extend("com.test.controller.App", {
    onInit: function() {
        if (checkSomething)) {
            // here call my first controller
        } else {
            // here call my second controller
            };
        }
    },
  });
});

and I have my second controller which I want to call only if that one if-statement fails

sap.ui.define(["sap/ui/core/mvc/Controller", "sap/m/MessageBox"], function(Controller, MessageBox) {
"use strict";
  return Controller.extend("com.test.Controller1", {
       onInit: function() {
           this.oRessourceBundle = this.getOwnerComponent().getModel("i18n").getResourceBundle();
       }
    });
});

here my second controller:

sap.ui.define(["sap/ui/core/mvc/Controller", "sap/m/MessageBox"    ],function(Controller, MessageBox) {
"use strict";
    return Controller.extend("com.test.Controller2", {
        onInit: function() {
           this.oRessourceBundle = this.getOwnerComponent().getModel("i18n").getResourceBundle();
        }
    });
});

I have a view for both controller so I don't copy it because it is empty so far anyway so there is also a

App.view.xml Controller1.view.xml Controller2.view.xml

how do I tell my appcontroller to call different controllers ?

I have also implemented the routes in the manifest.json file

Upvotes: 0

Views: 327

Answers (2)

Nali
Nali

Reputation: 157

since my old answer was deleted with some saying "this is not the answer to the question of the author" (while with all respect I was the author and therefore I guess I know if this solved my issue or not) I will explain what is explained in the link I posted:

this is what is in my index.html file:

 <script>
sap.ui.getCore().attachInit(function () {
            sap.ui.require([
                "sap/m/Shell",
                "sap/ui/core/ComponentContainer"
            ], function (Shell, ComponentContainer) {

            new Shell({                             // placing everything in shell
                app: new ComponentContainer({       // component container which holds component file
                    name: "sap.ui.iyc",             // root location of app
                    height: "100%"
                })
            }).placeAt("content");
        });
    });

Component.js This is a file which contains the configuration for router. After configuration and declaring routes and targets we have to initialize() the router in init() function as shown in below code snippet.

sap.ui.define([
"sap/ui/core/UIComponent"
], function (UIComponent) {
"use strict";

return UIComponent.extend("sap.ui.iyc.Component", {

    metadata: {

    "rootView": "sap.ui.iyc.routepages.App",        // initial view

    "routing": {
         "config": {
            "routerClass": "sap.m.routing.Router",  // router class
            "viewType": "XML",                      // types of views using in app
            "viewPath": "sap.ui.iyc.routepages",            // folder of views
            "controlId": "app",                     // container where pages are placed while navigating 
            "controlAggregation": "pages",          // contents which needs to be replced while navigating
            "transition": "slide"                   // navigation transition effect
         },

        "routes": [{                                // defining routes
            "pattern": "",                          // pattern of the URL
            "name": "first",                        // name of route
            "target": "first"                       // name of target
        },
        {
            "pattern": "second",
            "name": "second",
            "target": "second"
        }],

         "targets": {                               // defining targets
            "first": {                              // route name
                "viewName": "First"                 // target view name, will be navigated to this view
            },
            "second": {
                "viewName": "Second"
            }
         }

        }
        },

    init: function () {
        // call the init function of the parent
        UIComponent.prototype.init.apply(this, arguments);  // calling parent UIComponents

        // create the views based on the url/hash
        this.getRouter().initialize();                      // initializing router
    }

});

});

first controller:

sap.ui.define([
"sap/ui/core/mvc/Controller"
], function (Controller) {
"use strict";

return Controller.extend("sap.ui.iyc.routepages.First", {

    // getting router which is declared in component file

    getRouter : function () {
        return sap.ui.core.UIComponent.getRouterFor(this);  
    },

    // this function will trigger when button is clicked

    navToSecond : function (oEvent){
        this.getRouter().navTo("second");   // calls route name "second"
    }

});

});

second controller:

sap.ui.define([
"sap/ui/core/mvc/Controller"
], function (Controller) {
"use strict";

return Controller.extend("sap.ui.iyc.routepages.Second", {

    getRouter : function () {
        return sap.ui.core.UIComponent.getRouterFor(this);
    },

    // this function will trigger when button is clicked

    navToFirst: function() {
        this.getRouter().navTo("first");    // calls route name "first"
    }

});

});

for an even better understanding visit the following link: http://www.inkyourcode.com/how-to-navigate-between-two-views-using-routing/

and of course in your views you will have to implement buttons or whatever you want to trigger those functions which will route you to your views

Upvotes: 0

techippo.com
techippo.com

Reputation: 255

You can call other controller methods like below

sap.ui.controller("com.test.Controller2").yourMethodName();

Upvotes: 1

Related Questions