导航到其他控制器表单应用程序控制器

时间:2017-09-12 10:42:05

标签: javascript sapui5

所以我在sapui5中总是有我的app控制器:

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
            };
        }
    },
  });
});

我有第二个控制器,只有当一个if语句失败时我才想调用它

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();
       }
    });
});

这是我的第二个控制器:

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();
        }
    });
});

我有两个控制器的视图所以我不复制它,因为它到目前为止都是空的,所以还有一个

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

如何告诉appcontroller调用不同的控制器?

我还在manifest.json文件中实现了路由

2 个答案:

答案 0 :(得分:1)

您可以调用其他控制器方法,如下所示

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

答案 1 :(得分:0)

因为我的旧答案被删除了一些说法"这不是作者问题的答案" (尽管我是作者,但我想我知道这是否解决了我的问题)我将解释我发布的链接中解释的内容:

这是我的index.html文件中的内容:

 <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这是一个包含路由器配置的文件。配置并声明路由和目标后,我们必须在init()函数中初始化()路由器,如下面的代码片段所示。

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
    }

});

});

第一个控制器:

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"
    }

});

});

第二个控制器:

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"
    }

});

});

为了更好地理解,请访问以下链接: http://www.inkyourcode.com/how-to-navigate-between-two-views-using-routing/

当然在您的观点中,您必须实现按钮或任何您想要触发这些功能的按钮,这些功能会将您引导至您的视图