远程和本地 OData 服务 [英] Remote and local OData service
问题描述
如何配置 manifest.json 文件,以便当我运行 mockserver(mockserver.html) 时它会转到本地 json 数据,当我运行 index.html(应用程序的主要入口)时它会转到远程服务.我有一个来自文档的示例 manifest.json 文件,但不太清楚远程和本地服务如何发挥作用.
<代码>{"_version": "1.1.0",sap.app":{"_version": "1.1.0","id": "xxx","类型": "应用程序","title": "{{appTitle}}","description": "{{appDescription}}",应用程序版本":{版本":1.0.0"},数据源": {主服务":{"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/","type": "OData",设置":{"odataVersion": "2.0","localUri": "localService/metadata.xml"}}}},sap.ui":{"_version": "1.1.0","技术": "UI5",图标":{图标": "","收藏夹": "",电话": "","电话@2": "",药片": "",平板电脑@2":"},设备类型":{桌面":真的,平板电脑":真的,电话":真实},支持的主题":["sap_hcb",sap_bluecrystal"]},sap.ui5":{"_version": "1.1.0",根视图":{"viewName": "xxx.view.Main",类型":XML"},依赖关系":{"minUI5Version": "1.30.0",库":{"sap.ui.core": {},"sap.m": {},sap.ui.layout":{}}},内容密度":{紧凑":真的,舒适":真实},配置":{"productLocal": "localService/mockdata/products.json","productRemote": "/某个远程端点"},产品": {数据源":主服务"}}
}
控制器代码
var oModel = new sap.ui.model.odata.ODataModel("/", true);//var 数据 = oModel;//console.log(数据);var inputModel = new JSONModel("../model/inputs.json");var productsModel = new JSONModel();oModel.read("/ProductSet",空值,空值,错误的,函数_OnSuccess(oData,响应){控制台日志(oData);控制台日志(响应);var data = {"ProductCollection": oData.results};productsModel.setData(data);},函数_OnError(错误){控制台日志(错误);});//将模型设置为当前的xml视图this.getView().setModel(inputModel, "inputModel");this.getView().setModel(productsModel);
感谢您的帮助.
你不需要接触 manifest.json
文件来模拟服务.事实上,在manifest.json
中dataSources的属性-localUri
:获取本地元数据文档或注解uri的相对URL.不是为了服务.
我希望你的 mockserver.html
看起来像这样:
<头><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta charset="utf-8"><title>MockServerTutorial</title><script id="sap-ui-bootstrap"src="../../resources/sap-ui-core.js"data-sap-ui-libs="sap.m"data-sap-ui-theme="sap_bluecrystal"data-sap-ui-xx-bindingSyntax="complex"data-sap-ui-resourceroots='{"sap.ui.demo.MockServer": "../"}'><link rel="stylesheet" type="text/css" href="../css/style.css"><脚本>sap.ui.getCore().attachInit(function() {sap.ui.require(["sap/ui/demo/MockServer/localService/mockserver","树液/米/壳",sap/ui/core/ComponentContainer"]、函数(mockserver、Shell、ComponentContainer){模拟服务器.init();新壳({应用程序:新 sap.ui.core.ComponentContainer({高度:100%",名称:模拟服务器教程"})}).placeAt("内容");});});头部><body class="sapUiBody" id="content"></html>
您可以如下定义您的模拟服务器:
sap.ui.define([sap/ui/core/util/MockServer"], 函数(MockServer) {严格使用";返回 {/*** 初始化模拟服务器.* 您可以使用 URL 参数serverDelay"配置延迟.* 返回此文件夹中的本地模拟数据而不是真实数据进行测试.* @上市*/初始化:函数(){//创造var oMockServer = 新 MockServer({rootUri: "/"});//模拟元数据和模拟数据oMockServer.simulate("../localService/metadata.xml", {sMockdataBaseUrl: "../localService/mockdata",bGenerateMissingMockData: 真});//开始oMockServer.start();jQuery.sap.log.info("使用模拟数据运行应用程序");}};});
<块引用>
请确保您将处理自定义网址(带过滤器/排序)和在 mockserver.js
中正确导入函数.
How can I configure manifest.json file so when I run mockserver(mockserver.html) then it goes to the local json data and when I run index.html (main entry to application) then it goes to the remote service. I have a sample manifest.json file from the documentation but not very clear how remote and local service come into play.
{
"_version": "1.1.0",
"sap.app": {
"_version": "1.1.0",
"id": "xxx",
"type": "application",
"title": "{{appTitle}}",
"description": "{{appDescription}}",
"applicationVersion": {
"version": "1.0.0"
},
"dataSources": {
"mainService": {
"uri": "https://services.odata.org/V2/Northwind/Northwind.svc/",
"type": "OData",
"settings": {
"odataVersion": "2.0",
"localUri": "localService/metadata.xml"
}
}
}
},
"sap.ui": {
"_version": "1.1.0",
"technology": "UI5",
"icons": {
"icon": "",
"favIcon": "",
"phone": "",
"phone@2": "",
"tablet": "",
"tablet@2": ""
},
"deviceTypes": {
"desktop": true,
"tablet": true,
"phone": true
},
"supportedThemes": [
"sap_hcb",
"sap_bluecrystal"
]
},
"sap.ui5": {
"_version": "1.1.0",
"rootView": {
"viewName": "xxx.view.Main",
"type": "XML"
},
"dependencies": {
"minUI5Version": "1.30.0",
"libs": {
"sap.ui.core": {},
"sap.m": {},
"sap.ui.layout": {}
}
},
"contentDensities": {
"compact": true,
"cozy": true
},
"config": {
"productLocal": "localService/mockdata/products.json",
"productRemote": "/some remote end point"
},
"products": {
"dataSource": "mainService"
}
}
}
Controller code
var oModel = new sap.ui.model.odata.ODataModel("/", true);
//var data = oModel;
//console.log(data);
var inputModel = new JSONModel("../model/inputs.json");
var productsModel = new JSONModel();
oModel.read("/ProductSet",
null,
null,
false,
function _OnSuccess(oData, response) {
console.log(oData);
console.log(response);
var data = {"ProductCollection" : oData.results};
productsModel.setData(data);
},
function _OnError(error) {
console.log(error);
});
//set model(s) to current xml view
this.getView().setModel(inputModel, "inputModel");
this.getView().setModel(productsModel);
Thanks for the help.
You don't need to touch manifest.json
file for mocking the service.
In fact, in manifest.json
the property of dataSources- localUri
: takes relative URL to local metadata document or annotation uri. Not for the service.
I hope your mockserver.html
looks like this:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<title>MockServerTutorial</title>
<script id="sap-ui-bootstrap"
src="../../resources/sap-ui-core.js"
data-sap-ui-libs="sap.m"
data-sap-ui-theme="sap_bluecrystal"
data-sap-ui-xx-bindingSyntax="complex"
data-sap-ui-resourceroots='{"sap.ui.demo.MockServer": "../"}'>
</script>
<link rel="stylesheet" type="text/css" href="../css/style.css">
<script>
sap.ui.getCore().attachInit(function() {
sap.ui.require([
"sap/ui/demo/MockServer/localService/mockserver",
"sap/m/Shell",
"sap/ui/core/ComponentContainer"
], function (mockserver, Shell, ComponentContainer) {
mockserver.init();
new Shell({
app: new sap.ui.core.ComponentContainer({
height : "100%",
name : "MockServerTutorial"
})
}).placeAt("content");
});
});
</script>
</head>
<body class="sapUiBody" id="content">
</body>
</html>
You can define your mock server as below:
sap.ui.define([
"sap/ui/core/util/MockServer"
], function(MockServer) {
"use strict";
return {
/**
* Initializes the mock server.
* You can configure the delay with the URL parameter "serverDelay".
* The local mock data in this folder is returned instead of the real data for testing.
* @public
*/
init: function() {
// create
var oMockServer = new MockServer({
rootUri: "/"
});
// simulate against the metadata and mock data
oMockServer.simulate("../localService/metadata.xml", {
sMockdataBaseUrl: "../localService/mockdata",
bGenerateMissingMockData: true
});
// start
oMockServer.start();
jQuery.sap.log.info("Running the app with mock data");
}
};
});
Please make sure you will handle custom urls (with filters/sorts) and function import properly in
mockserver.js
.
这篇关于远程和本地 OData 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!