SAPUI5 (33) - 使用 SAP Web IDE 提供的代理服务

前面我们访问 Northwind OData service,使用的是 https://cors-anywhere.herokuapp.com/ 提供的代理服务。其实,如果在 SAP Web IDE 中编写代码的话,Web IDE 也提供了代理服务。本篇我们就来看看如何使用 SAP Web IDE 提供的代理服务。

使用 Web IDE 配置连接

进入在线版 Web IDE,登陆后找到左边 Panel 下的 Connectivity -> Destinations:

Paste_Image.png

点击 New Destination,然后设置如下:

点击 New Property,设置如下属性:

保存后,点击 Check connection,如果出现成功提示,则表示连接成功。

Web IDE 的这个 Destination 设置,可以用于该账户下的所有项目。 如果配置了 destination, IDE
中的项目 serviceUrl 的写法和之前通过代理的方法不同。比如,我们之前的连接 url 为:

var sServiceUrl = proxy + "http://services.odata.org/V2/(S(tigt4zfne0egj3u25bhqq32a))/OData/OData.svc/"

现在这个 serviceUrl 应该这样写:

var sServiceUrl = "/destinations/northwind/V2/(S(vitw3mhyp5ifhga2mttx5okl))/OData/OData.svc"

Host 和 port 被替换成 /destinations/northwind/

SAP Web IDE 连接测试

在 SAP Web IDE 中创建一个类型为 SAPUI5 Applicaation 类型的项目,IDE 预设的项目文件结构为:

在项目的根目录下多了 neo-app.json 文件和 .project.json 文件。neo-app.json 文件中包含了引用的 destination :

{
    "path": "/destinations/northwind",
    "target": {
    "type": "destination",
    "name": "northwind"
    },
    "description": "Northwind OData Service"
}      

代码测试:

// Application data
var sServiceUrl = "/destinations/northwind/V2/(S(vitw3mhyp5ifhga2mttx5okl))/OData/OData.svc"
var oModel = new sap.ui.model.odata.v2.ODataModel({
    serviceUrl: sServiceUrl
});
oModel.setUseBatch(false);

function readTest(){
    oModel.read("/Suppliers(0)", {
        success: function(oData, oResponse){
            console.log(oResponse);
            console.log(oData);
        },
        error: function(oError){
            console.log(oError);
        }
    });
}

var oButton = new sap.m.Button({
    text: "Read first supplier",
    press: readTest
});

Personal Edition 如何使用代理

因为 SAP Web IDE 只是 SAP Cloud Platform (简称为 HCP) 中 Service 下面的一个子功能,而 Destination 的配置是在 Connectivity - Destinations 下面,所以个人版中是没有这个设置界面的。那么,个人版能不能使用这个代码功能呢?答案是可以,方法如下:

将 Web IDE 在线版的 Destination 文件下载:

然后,在 SAP Web IDE 个人版的安装目录下,找到子文件夹:config_master\service.destinations\destinations,将这个文件放进去就可以了。

注意这个文件不能有扩展名,如果放进去的时候本地服务已经启用,需要退出重新启用服务。这个 destination 文件是一个类似 ini 的配置文件,我们可以用文本编辑器打开编写,也就是说,我们也可以手工编辑这个文件。

#
#Sat Apr 22 13:40:42 UTC 2017
Description=Northwind
Type=HTTP
Authentication=NoAuthentication
WebIDEUsage=odata_gen
Name=northwind
WebIDEEnabled=true
CloudConnectorVersion=2
URL=http\://services.odata.org
ProxyType=Internet
WebIDESystem=Northwind

参考

Create a Northwind Destination

Connecting Remote Systems in SAP Web IDE Personal Edition

Step 26: Remote OData Service

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值