由于声明的变量(idno,namecity)在括号内,因此我无法从外部访问它们。这是获取价值的方法还是最好的方法。谢谢
<script>
var app = sap.m.App("myApp",{});
var url = "proxy/http/server/ZCUST_TESTING_SRV /?$filter=IKunnr eq '800COL101'";
var username = "mobtest";
var password = "welcome1";
var oModel = new sap.ui.model.odata.ODataModel(url, true, username, password);
oModel.read('/', null, null, true, function(oData, oResponse)
{
var dataget = JSON.stringify(oData);
var count = oData.results[0].Ort01;
var namecity= oData.results[0].Name1;
var idno= oData.results[0].Kunnr;
});
var l4 = new sap.m.Label("l4",{text: count});
var l5 = new sap.m.Label("l5",{text: namecity});
var l6 = new sap.m.Label("l6",{text: idno});
var page = new sap.m.Page("page",{
title:"Address Details",
showNavButton:true,
navButtonTap: function(){
//app.back();
app.to("Page");
},
content: [ l4,l5,l6, new sap.m.Button({text:"submit" }})]
});
app.addPage(page);
app.placeAt('content');
</script>
最佳答案
根据对您的问题的评论中的讨论,我为您提供了一个示例,该示例更恰当地使用ODataModel,而不是与之抗争,不必进行显式读取,不直接访问results
属性,并且不捕获手动检索数据并将其放入新的JSON模型中。
基本上,实例化ODataModel,对控件属性使用绑定语法,并设置适当的绑定上下文,以便解析相对路径。
Here's the JS Bin example。我使用XML编写了UI控件,因为它更加简洁,并使用了Northwind模型,但尝试保持原始目标。
以下是该示例的摘要:
<App>
<Page
binding="{/Employees(1)}"
title="Address Details"
showNavButton="true">
<content>
<Label text="{TitleOfCourtesy}" />
<Label text="{FirstName}" />
<Label text="{LastName}" />
</content>
</Page>
</App>
请注意页面上的绑定,以及“标签”文本属性中的绑定。
oView
.setModel(new sap.ui.model.odata.ODataModel(
"http://cors-anywhere.herokuapp.com/http://services.odata.org/V3/Northwind/Northwind.svc/",
{
json : true,
maxDataServiceVersion : "2.0"
}
))
请注意ODataModel的设置方式,并注意不存在任何
read()
调用,不存在试图捕获和存储数据的任何回调成功处理程序,以及不存在任何辅助JSON模型。 (此外,我正在使用非常有用的cors-anywhere服务,因此我们可以查看不使用代理等的远程OData服务的示例)。