0

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>
4
  • Why are you doing this? It is becoming an anti-pattern. Let the toolkit do the work; assign the model to the control tree and just use data binding.
    – qmacro
    Aug 25, 2014 at 13:21
  • I am new to sapui5. can you give me an example
    – James
    Aug 25, 2014 at 13:24
  • Example given (see my answer below)
    – qmacro
    Aug 26, 2014 at 4:46
  • This question, with almost the same code, was also asked on SCN yesterday - scn.sap.com/thread/3608919 - was that you also?
    – qmacro
    Aug 26, 2014 at 12:09

2 Answers 2

1

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).

0

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

4
  • Thank You Sir I can get it know.
    – James
    Aug 26, 2014 at 5:16
  • 1
    Allen, I humbly suggest you don't encourage this sort of thing - creating a JSON Model in a success callback from an explicit OData Model read call is, at least in my view, bad practice.
    – qmacro
    Aug 26, 2014 at 5:28
  • Hi qmacro, could you please elaborate?
    – Haojie
    Aug 26, 2014 at 6:14
  • See the answer below for a better approach.
    – qmacro
    Aug 27, 2014 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.