如何将Controller作为依赖项传递?

时间:2016-10-31 21:04:17

标签: dependency-injection sap sapui5

我正在寻找创建一个测试文件(test.js)。 Test.js文件的目的是为特定的控制器执行单元测试(即:Main.controller.js)。

如何将此控制器加载到外部js文件?

我尝试过使用sap.require:

sap.ui.require(["/pricingTool/Controller/Main.controller"],

 function(Main){
     //Quint code
     test("hello test", function(assert) {
      assert.ok(1 == "1", "Passed!");
    });
});

但我收到的错误是:

无法加载/Controller/Main.controller.js

这告诉我,我要么构造错误,使用错误的路径,要么两者兼而有之。任何的意见都将会有帮助。我在下面附上了我的文件树以供参考。

enter image description here

Component.js

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

    var Component = UIComponent.extend("pricingTool.Component", {

        metadata : {

            metadata : {
                maniest: "json"
            },
            rootView : "pricingTool.view.Main",
            dependencies : {
                libs : [
                    "sap.m",
                    "sap.ui.layout"
                ]
            },
            config : {
                sample : {
                    files : [
                        "Main.view.xml",
                        "Main.controller.js"
                    ]
                }
            }
        },

        init : function () {
            // call the init function of the parent
            UIComponent.prototype.init.apply(this, arguments);
            // additional initialization can be done here
        }
    });

    return Component;

});

的index.html

<!DOCTYPE HTML>
<html>

    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta charset="utf-8">

        <title>Pricing Tool</title>

        <script id="sap-ui-bootstrap"
            src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
            data-sap-ui-libs="sap.m"
            data-sap-ui-theme="sap_belize"
            data-sap-ui-xx-bindingSyntax="complex"
            data-sap-ui-preload="async"
            data-sap-ui-compatVersion="edge" 
            data-sap-ui-resourceroots='{"pricingTool": "./"}'>
        </script>
        <script src='../pdfmake-master/build/pdfmake.min.js'></script>
        <script src='../pdfmake-master/build/vfs_fonts.js'></script>

        <!-- Application launch configuration -->
        <script>

            sap.ui.getCore().attachInit(function() {
                new sap.m.App ({
                    pages: [
                        new sap.m.Page({
                            title: "Pricing Tool Rapid Prototype", 
                            enableScrolling : true,
                            content: [ new sap.ui.core.ComponentContainer({
                                name : "pricingTool"
                            })]
                        })
                    ]
                }).placeAt("content");
            });

        </script>
    </head>

    <!-- UI Content -->
    <body class="sapUiBody" id="content" role="application">
    </body>

</html>

initialTest.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>QUnit Example</title>
  <link rel="stylesheet" href="//code.jquery.com/qunit/qunit-1.15.0.css">
      <script id="sap-ui-bootstrap"
                src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js">
      </script>    
      <script src="//code.jquery.com/qunit/qunit-1.15.0.js"></script>
      <script src="allTests.js"></script>
      <script src="/Controller/Main.controller.js"></script>          
      <script>
      </script>
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您无需直接在.html文件中引用控制器文件。

问题是UI5不知道在哪里可以找到您的资源。解决过程如下:

  • 所有标准资源都是从相对于加载sap-ui-core.js的路径加载的。
  • 基于资源根定义加载具有自定义命名空间的所有资源。

基本上,在资源根目录中,您可以在名称空间前缀和查找以该前缀开头的资源的位置之间定义映射。如果使用相对路径,则相对于html文件解析位置。

index.html你已经完成了这个:data-sap-ui-resourceroots='{"pricingTool": "./"}'这完全没问题。由于您的index.thml位于项目的根目录,因此请求pricingTool/Controller/Main.controller将导致在./Controller/Main.controller处发出请求,这实际上是我们的文件。

但在你的initialTest.html中,你根本没有指定任何资源根。此外,您直接引用了控制器并且路径似乎不正确(请注意,以前导/开头的路径被视为当前主机上的绝对路径;根据您使用的服务器,这可能不是是正确的,因为通常每个应用程序在服务器中都有自己的子路径。

因此,您应在data-sap-ui-resourceroots='{"pricingTool": "../"}'内添加initialTest.html。请注意,我使用了..,因为html文件位于Test文件夹中(其他内容位于兄弟文件夹中)。

相关问题