简体   繁体   English

SAPUI5:如何在运行时创建XMLView及其控制器?

[英]SAPUI5: How to create XMLView and its controller in runtime?

I want to create an XMLView in runtime and bind a controller to it. 我想在运行时中创建一个XMLView并将一个控制器绑定到它。 But I don't know how to create and instance of the controller in runtime. 但是我不知道如何在运行时创建控制器实例。

Here is the code that I wrote: 这是我编写的代码:

var oView = sap.ui.xmlview(sViewId, {
            viewContent: sViewContent,
            controller: ?????
        });
oRouter.getViews().setView(sViewName, oView);

Here is the API that I wrote this code based on: 这是我根据以下代码编写此代码的API:

https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.html#.xmlview https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.html#.xmlview

There are two main ways of doing it. 有两种主要方法。 You can either define a new controller class on the fly and then use it to instantiate a new controller instance: 您可以动态定义一个新的控制器类,然后使用它实例化一个新的控制器实例:

var MyController = sap.ui.core.mvc.Controller.extend("MyController", {
    onInit: function() {
        // do something
    }
});
var oView = sap.ui.xmlview(sViewId, {
     viewContent: sViewContent,
     controller: new MyController()
});

Or you can use the sap.ui.controller function to define a new controller and then to obtain an instance of it: 或者,您可以使用sap.ui.controller函数定义一个新的控制器,然后获取它的实例:

// first define the new controller type
sap.ui.controller("MyController", {
    onInit: function() {
        // do something
    }
});
var oView = sap.ui.xmlview(sViewId, {
     viewContent: sViewContent,
     // then instantiate the controller
     controller: sap.ui.controller("MyController")
});

If inside the view XML content you reference towards the controller name, then you don't necessarily have to pass the controller instance to the sap.ui.xmlview function call. 如果在视图XML内容内您引用了控制器名称,则不必将控制器实例传递给sap.ui.xmlview函数调用。 Eg you can look at the SAPUI5 jsfiddle template: https://jsfiddle.net/nistv4n/93mx0yvt/ 例如,您可以看一下SAPUI5 jsfiddle模板: https ://jsfiddle.net/nistv4n/93mx0yvt/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM