我得到了一个捆绑包,里面有一些我需要的数据:
"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的实例
- 将数据传递给那个实例(我已经在构造函数中完成了)
- 最终将模型放在视图中或您想要的位置
这应该行得通。