0

Can you help me solve this mystery? I got the walkthrough example in step 10 and adapted it so I can create a simple binding to an OData service.

Let me share the main files:

Component.js

sap.ui.define([
  "sap/ui/core/UIComponent",
  "sap/ui/model/json/JSONModel"
], function(UIComponent, JSONModel) {
  "use strict";

  return UIComponent.extend("BasicMessages.Component", {
    metadata: {
      manifest: "json"
    },

    init: function() {
      // call the init function of the parent
      UIComponent.prototype.init.apply(this, arguments);
      // set data model
      var oData = {
        recipient: {
          name: "World"
        }
      };
      var oModel = new JSONModel(oData);
      this.setModel(oModel, "json");
    },

  });
});

Manifest.json (hid the service URI, but it's properly configured)

{
  "_version": "1.8.0",
  "sap.app": {
    "id": "BasicMessages",
    "type": "application",
    "i18n": "i18n/i18n.properties",
    "title": "{{appTitle}}",
    "description": "{{appDescription}}",
    "applicationVersion": {
      "version": "1.0.0"
    },
    "dataSources": {
      "mainService": {
        "uri": "hidden_service_uri",
        "type": "OData",
        "settings": {
          "odataVersion": "2.0",
          "localUri": "localService/metadata.xml"
        }
      }
    }
  },
  "sap.ui": {
    "technology": "UI5",
    "deviceTypes": {
      "desktop": true,
      "tablet": true,
      "phone": true
    }
  },
  "sap.ui5": {
    "rootView": {
      "viewName": "BasicMessages.view.App",
      "type": "XML",
      "async": true,
      "id": "app"
    },
    "dependencies": {
      "minUI5Version": "1.30",
      "libs": {
        "sap.m": {}
      }
    },
    "models": {
      "i18n": {
        "type": "sap.ui.model.resource.ResourceModel",
        "settings": {
          "bundleName": "BasicMessages.i18n.i18n"
        }
      },
      "": {
        "dataSource": "mainService",
        "settings": {
          "defaultUpdateMethod": "PUT",
          "useBatch": false,
          "metadataUrlParams": {
            "sap-documentation": "heading"
          },
          "defaultBindingMode": "TwoWay"
        }
      }
    }
  }
}

App.controller.js

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/m/MessageToast"
], function(Controller, MessageToast) {
  "use strict";
  
  return Controller.extend("BasicMessages.controller.App", {
    onInit: function() {
      this.getView().setModel(this.getOwnerComponent().getModel());
    },

    onShowHello: function() {
      // read msg from i18n model
      var oBundle = this.getView().getModel("i18n").getResourceBundle();
      var sRecipient = this.getView().getModel("json").getProperty("/recipient/name");
      var sMsg = oBundle.getText("helloMsg", [sRecipient]);
      // show message
      MessageToast.show(sMsg);
    },

  });
});

App.view.xml (this is the tricky file)

<mvc:View controllerName="BasicMessages.controller.App"
  xmlns:mvc="sap.ui.core.mvc"
  xmlns="sap.m">
  <Button text="{i18n>showHelloButtonText}" press="onShowHello" />
  <Input value="{json>/recipient/name}"
    description="Hello {json>/recipient/name}"
    valueLiveUpdate="true" width="60%"
  />
  <Text text="{/Caixas(Id='0001',InicioValidade='20180712193002')/Descricao}" />
  <List items="{/Caixas}">
    <StandardListItem title="{Descricao}" />
  </List>
</mvc:View>

If I use the view file exactly like that, it calls the OData service on the backend and retrieve the list and the text element contents.

link to screenshot:
screenshot 1

If I comment the list lines, the OData service won't be called and the text element won't be filled with the text.

link to screenshot:
screenshot 2

Why does this happen???

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
  • Does this answer your question? [bindProperty to Single OData Entity](https://stackoverflow.com/questions/23956091/bindproperty-to-single-odata-entity) – Boghyon Hoffmann Jan 16 '21 at 01:04

2 Answers2

1

Got the answer on SAP Docs

Note: Requests to the back end are triggered by list bindings (ODataListBinding), element bindings (ODataContextBinding), and CRUD functions provided by the ODataModel. Property bindings (ODataPropertyBindings) do not trigger requests.

0

I think, Rafael explained why this happens (your question), now to answer the unasked question "howto make this work":

You would need to bind the Text control to the entitiy and the text property to the Descricio attribute, basically something like that:

<Text binding="{/Caixas(Id='0001',InicioValidade='20180712193002')" text="{Descricao}" />

(sorry, no ui5 runtime environment at hand, so I didnt actually run this)

iPirat
  • 2,197
  • 1
  • 17
  • 30