我得到了一些我需要的数据:
"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>
基本上,你
- 从某处获取数据
- 创建一个 JSONModel 实例
- 将数据传递给那个实例(我已经在构造函数中完成了)
- 最后将模型放在 View 上或任何你想要的地方
这应该有效。
关于javascript - SAPUI5 设置JSON模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131210/