简体   繁体   中英

How to get a ODATA value from the model and display it on a message box

I have created an ODATA Call to the Northwind database, I have been playing with bindings and have some questions that I could not figure out.

In this specific example when I click Login it is going to display the value in the text box, and also open a messagebox to display the same, but the very first time i click it won't display anything. After the second time it works. Why is this happening?

Another question is if I can change the bind of the textbox dynamically instead of using {CompanyName} I would like to get the ContactName but when I use SETVALUE it will print the text {ContactName}. Thanks again!

https://jsbin.com/nofunecane/1/edit?html,output

sap.m.MessageBox.alert(this.getView().getModel().getProperty("/Customers('ALFKI')/CompanyName"));

This is the new code with the Success Method: https://jsbin.com/gagajusoja/edit?html,output

I didn't quite get what was your intention, because what you are doing in the "getLogin" function is:

  1. take the Input control by id and do the element binding (to a specific customer). In the xml view you bind the value of the Input to the "CompanyName" property(relatively), that's why after clicking on "Log In" button, the input is automatically populated by the "Alfred Futterkiste" - the company name of the customer "ALFKI"
  2. on the next code line, you try to get the "CompanyName" property, and getting the empty message box, which is correct because at that time binding did not load the data form the backend (because normally, all the HTTP requests are asynchronous, so you need to wait some time until the data are loaded).

In order to do so, use the dataReceived callback when bindElement . So your function will look like this:

getLogin: function(evt) {
    var oModel = this.getView().getModel();
    this.byId("txtUid").bindElement({
        path: "/Customers('ALFKI')",
        events: {
            dataReceived: function(oResponse) {
                sap.m.MessageBox.alert(oModel.getProperty("/Customers('ALFKI')/CompanyName"));
            }
        }
    });

    this.getView().getModel().read("/Customers('ANATR')", {

        success: function(oResponse) {
            sap.m.MessageBox.alert(oModel.getProperty("/Customers('ANATR')/CompanyName"));
        }

    });
}

As for your 2nd question, you can use bindProperty method programmatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM