前面我們?cè)L問(wèn) Northwind OData service庞溜,使用的是 https://cors-anywhere.herokuapp.com/
提供的代理服務(wù)。其實(shí)嘹黔,如果在 SAP Web IDE 中編寫(xiě)代碼的話天通,Web IDE 也提供了代理服務(wù)。本篇我們就來(lái)看看如何使用 SAP Web IDE 提供的代理服務(wù)逼争。
使用 Web IDE 配置連接
進(jìn)入在線版 Web IDE优床,登陸后找到左邊 Panel 下的 Connectivity -> Destinations:
點(diǎn)擊 New Destination,然后設(shè)置如下:
點(diǎn)擊 New Property誓焦,設(shè)置如下屬性:
保存后胆敞,點(diǎn)擊 Check connection,如果出現(xiàn)成功提示杂伟,則表示連接成功移层。
Web IDE 的這個(gè) Destination 設(shè)置,可以用于該賬戶下的所有項(xiàng)目赫粥。 如果配置了 destination, IDE
中的項(xiàng)目 serviceUrl
的寫(xiě)法和之前通過(guò)代理的方法不同观话。比如,我們之前的連接 url 為:
var sServiceUrl = proxy + "http://services.odata.org/V2/(S(tigt4zfne0egj3u25bhqq32a))/OData/OData.svc/"
現(xiàn)在這個(gè) serviceUrl 應(yīng)該這樣寫(xiě):
var sServiceUrl = "/destinations/northwind/V2/(S(vitw3mhyp5ifhga2mttx5okl))/OData/OData.svc"
Host 和 port 被替換成 /destinations/northwind/
越平。
SAP Web IDE 連接測(cè)試
在 SAP Web IDE 中創(chuàng)建一個(gè)類型為 SAPUI5 Applicaation
類型的項(xiàng)目频蛔,IDE 預(yù)設(shè)的項(xiàng)目文件結(jié)構(gòu)為:
在項(xiàng)目的根目錄下多了 neo-app.json
文件和 .project.json
文件。neo-app.json
文件中包含了引用的 destination :
{
"path": "/destinations/northwind",
"target": {
"type": "destination",
"name": "northwind"
},
"description": "Northwind OData Service"
}
代碼測(cè)試:
// 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 如何使用代理
因?yàn)?SAP Web IDE 只是 SAP Cloud Platform (簡(jiǎn)稱為 HCP) 中 Service 下面的一個(gè)子功能喧笔,而 Destination 的配置是在 Connectivity - Destinations 下面帽驯,所以個(gè)人版中是沒(méi)有這個(gè)設(shè)置界面的。那么书闸,個(gè)人版能不能使用這個(gè)代碼功能呢尼变?答案是可以,方法如下:
將 Web IDE 在線版的 Destination 文件下載:
然后浆劲,在 SAP Web IDE 個(gè)人版的安裝目錄下嫌术,找到子文件夾:config_master\service.destinations\destinations
,將這個(gè)文件放進(jìn)去就可以了牌借。
注意這個(gè)文件不能有擴(kuò)展名度气,如果放進(jìn)去的時(shí)候本地服務(wù)已經(jīng)啟用,需要退出重新啟用服務(wù)膨报。這個(gè) destination 文件是一個(gè)類似 ini 的配置文件磷籍,我們可以用文本編輯器打開(kāi)編寫(xiě)适荣,也就是說(shuō),我們也可以手工編輯這個(gè)文件院领。
#
#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