since the variables declared(idno, namecity) are inside parentheses I could not access them out side. Is this the way to get values or which would be the best way. Thank You
<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>
Based on the discussion in the comments to your question, I put together for you an example of using the ODataModel more appropriately, not fighting against it, not having to do explicit reads, not accessing the results
property directly, and not capturing the retrieved data manually and putting it into a new JSON model.
Basically, instantiate the ODataModel, use binding syntax on your control properties, and set the appropriate binding context so that the relative paths resolve.
Here's the JS Bin example. I wrote the UI controls in XML as it's cleaner, and used the Northwind model, but otherwise tried to keep to your original goals.
Here are some snippets from that example:
<App>
<Page
binding="{/Employees(1)}"
title="Address Details"
showNavButton="true">
<content>
<Label text="{TitleOfCourtesy}" />
<Label text="{FirstName}" />
<Label text="{LastName}" />
</content>
</Page>
</App>
Note the binding on the Page, and the binding in the Label text properties.
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"
}
))
Note the way the ODataModel is set, and note the absence of any read()
calls, the absence of any callback success handlers trying to catch and store the data, and the absence of any helper JSON models. (Also, I'm using the very useful cors-anywhere service so we can look at an example of a remote OData service without using proxies etc).
You get the count, namecity and other variables outside the callback as "undefined" as the callback function is called after your OData call is finished successfully and those variables are only set at that time. You should do your UI rendering logic inside the callback and the best practice is to use data binding.
For example, your JSON string is as following:
{
"results":
[
{
"Ort01":"1",
"Name1":"2",
"Kunnr": "3"
}
]
}
Do the data binding as following:
var oModel = new sap.ui.model.odata.ODataModel(url, true, username, password);
var l4 = new sap.m.Label("l4",{text: "{/results/0/Ort01}"});
// {/results/0/Ort01} is the binding path
var l5 = new sap.m.Label("l5",{text: "{/results/0/Name1}"});
var l6 = new sap.m.Label("l6",{text: "{/results/0/Kunnr}"});
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');
oModel.read('/', null, null, true, function(oData, oResponse)
{
//Data binding
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(oData);
page.setModel(oModel);
});
This is your starting point. For details, please see the documentation and read code for the demo apps.
Regards,
Allen