javascript - SAPUI5 设置JSON模型

标签 javascript json html sapui5

我得到了一些我需要的数据:

"mpConfig": {
    "appId": "test",
    "isMock": "test",
    "mockServerPath": "test",
    "isDebug": "test",
    "isUnitTest": "test"
}

现在我需要将其放入 JSON 模型中以便能够使用该数据。要检索数据,我有我的 Controller 类,并且用法必须在 View 类中。我该如何处理这个问题?

我读到类似这样的东西将它们放入 JSON 模型中。

this.getView().setData(path().getConfig().mpConfig;

如何使用上述配置从 JavaScript 类获取的数据?感谢所有帮助。

最佳答案

下面的示例向您展示了如何创建 JSONModel 的新实例并将数据传递给它。我在示例中使用的数据是一个 JavaScript 对象。如果你有一个 JSON 字符串,请确保调用

var oData = JSON.parse(sMyJsonString);

下面的例子没有调用这个。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>SAPUI5 single file template | nabisoft</title>
        <script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
            id="sap-ui-bootstrap"
            data-sap-ui-theme="sap_bluecrystal"
            data-sap-ui-libs="sap.m"
            data-sap-ui-bindingSyntax="complex"
            data-sap-ui-compatVersion="edge"
            data-sap-ui-preload="async"></script>
            <!-- use "sync" or change the code below if you have issues -->

        <!-- XMLView -->
        <script id="myXmlView" type="ui5/xmlview">
            <mvc:View
                controllerName="MyController"
                xmlns="sap.m"
                xmlns:core="sap.ui.core"
                xmlns:mvc="sap.ui.core.mvc">

                <VBox>
                    <Text text="appId          : {/mpConfig/appId}" />
                    <Text text="isMock         : {/mpConfig/isMock}" />
                    <Text text="mockServerPath : {/mpConfig/mockServerPath}" />
                    <Text text="isDebug        : {/mpConfig/isDebug}" />
                    <Text text="isUnitTest     : {/mpConfig/isUnitTest}" />
                </VBox>

            </mvc:View>
        </script>

        <script>
            sap.ui.getCore().attachInit(function () {
                "use strict";

                //### Controller ###
                sap.ui.controller("MyController", {
                    onInit : function () {

                        var oData = {
                            "mpConfig": {
                                "appId": "test appId",
                                "isMock": "test isMock",
                                "mockServerPath": "test mockServerPath",
                                "isDebug": "test isDebug",
                                "isUnitTest": "test isUnitTest"
                            }
                        };

                        var oModel = new sap.ui.model.json.JSONModel(oData);
                        this.getView().setModel(oModel);
                    }
                });

                //### THE APP: place the XMLView somewhere into DOM ###
                sap.ui.xmlview({
                    viewContent : jQuery("#myXmlView").html()
                }).placeAt("content");

            });
        </script>

    </head>

    <body class="sapUiBody">
        <div id="content"></div>
    </body>
</html>

基本上,你

  1. 从某处获取数据
  2. 创建一个 JSONModel 实例
  3. 将数据传递给那个实例(我已经在构造函数中完成了)
  4. 最后将模型放在 View 上或任何你想要的地方

这应该有效。

关于javascript - SAPUI5 设置JSON模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131210/

相关文章:

arrays - 在dart中解析对象(不支持的操作:无法添加到定长列表)

html - 从字体到跨度(大小和颜色)和背面的正则表达式(VB.NET)

html - 使页脚被内容推离页面,但在不是整页内容时留在底部

javascript - jQuery解析json并获取元素,得到未定义

html - Bootstrap 导航栏在移动 View 中不会缩小

javascript - 使用纯 JS 在点击期间使 div 相互堆叠

javascript - 为什么未定义从 index.tsx 文件导出? ( react + typescript )

javascript - 如何创建 iframe 然后使用 jQuery 将 javascript 附加到它?

javascript - 为什么我的 ajax 调用不起作用?

jQuery 1.3.2 + JSON + 跨域: How to filter the returned data?