SAP / openui5

OpenUI5 lets you build enterprise-ready web applications, responsive to all devices, running on almost any browser of your choice.
http://openui5.org
Apache License 2.0
2.95k stars 1.23k forks source link

Odata V4 element binding not working #1206

Closed apazureck closed 7 years ago

apazureck commented 7 years ago

OpenUI5 version: 1.40.6

Browser/version (+device/version): ie/chrome/firefox

Steps to reproduce the problem:

  1. Create an XML View with a form
  2. Create some Binding in XML e.g. a form with binding=myodatamodel>/MyItems(1)}", or in codebehind with an onInit Method: .byId("myform").bindElement("myodatamodel>/MyItems(1)");
  3. Create a form element with a relative binding (for example <Text text="{myodatamodel>ItemName}"/>)

What is the expected result? The text field in the form should display the Item name.

What happens instead? It trys to append the ItemName binding to the odata Request: http://myodataservice.com:8000/myservice.svc/MyItems(1)/ItemName

I read the information here: https://openui5.hana.ondemand.com/#docs/guide/54e0ddf695af4a6c978472cecb01c64d.html

It told me it is possible this way, but it is not, obviously. What is going wrong here or am not getting something right?

EDIT:

ThomasChadzelek commented 7 years ago

Hello!

Isn't it called "binding" inside an XML view, e.g. <form:SimpleForm binding="{myodatamodel>/MyItems(1)}">?! But "bindElement" in JavaScript should be OK.

It's hard to tell what goes wrong here from the given information. Basically, that's how it should work and how we use it ourselves in some examples.

Is there an easy way for you to share more information, e.g. complete files?

Best regards, Thomas

SeeSharpSoft commented 7 years ago

Hi,

I neither used the XML binding syntax nor did I use ODataV4 binding mechanism, but the (usual) Element.bindElement syntax is without curly braces:

view.byId("myform").bindElement("myodatamodel>/MyItems(1)");

Can the ODataV4 developer guide be wrong in this regards?

(I also found this example page which indicates that the XML syntax is with curly braces and should be therefore correct (as Thomas mentioned with attribute "binding" instead of "bind"): https://openui5.hana.ondemand.com/#docs/guide/91f05e8b6f4d1014b6dd926db0e91070.html)

ThomasChadzelek commented 7 years ago

SeeSharpSoft is right, of course. You can call bindElement in two ways, with a simple string or with a parameters object. In both cases, the path is just a path without curly braces.

In contrast, the "binding" attribute in an XML view uses binding syntax with curly braces.

apazureck commented 7 years ago

@ThomasChadzelek & @SeeSharpSoft: Sorry, you are right of course. It is called binding. This was a typo. At last I was not sure, which syntax was correct, as neither of them worked :)

Ok, let me rephrase my question here. Would it be possible to get some more extended examples on the odata topic. As I understand it and my colleagues told me, odata is the recommended way to go (odata v2 at least). SAP working on the standards on odata together with MS second that.

Sadly I am not allowed to publish any code, as it is from my employer. I will try to make an example service these days. I just hoped somebody had a similar issue with the binding.

Here some description, what were my steps:

  1. I have a set of entities (ListA), which are published by my odata service (ASP.NET - odata v4). These Items have a reference (key only) to another entity set (ListB) on my service. They have both a Name property, which is a text.
  2. I reference my service as a named model ("ams") in my manifest.json.
  3. I bound them to a sap.m.Table (items). <Table items="{ams>/ListA}" select="onSelectionChange">...</Table>
  4. The user can now click on the table entry and a detail view will pop up. Therefore, I created an xml view, which is nested in the same page as my table is.
  5. In my controller I always change the conext .setBindingContext(selectedItemData, "ams"); of my detail view.
  6. In my detail view I have some sap.m.Text elements to display the data of the selected Item of ListA, which works just fine: <Text text="{ams>Name}"/>

What I wanted to do next ist something like that: <Text binding="{ams>/ListB(1)}" text="{ams>Name}"/> or <Panel binding="{ams>/ListB(1)}"><Text text="{ams>Name}"/>...(further Text)...</Panel> So I expected to get the name of the first item in ListB Later I wanted to change that 1 to an expression binding, which should look something like that: {= {ams>ItemBId}}, to get the reference in my first item.

So my question here is: Can you tell me, how to bind such a thing correctly? A possible scenario would be to use this, if you have two odata services or another model. There you cannot use $expand. In my case I just looked for a quick work around, as i did not want to change the odata service.

Thank you and best regards

ThomasChadzelek commented 7 years ago

Here's what one of my colleagues has to say about this.

Usually, one would use a navigation property here instead of having the key properties of ListB repeated in ListA. If the service however does not use a navigation property and cannot be changed you have to set the binding of the Panel to display the ListB entity corresponding to the selected ListA entity in onSelectionChange:

In the xml view, add an id to the panel control so that you can access it in controller coding <Panel id=“myPanel“>

In controller coding, add sap/ui/model/ODataUtils to the imported modules as ODataUtils

onSelectionChange : function (oEvent) {
  var sItemBId = oEvent.getParameters().listItem.getBindingContext().getProperty(“ItemBId”);

  // Note: the call to ODataUtils should only be done if the itemBId has the OData type Edm.String
  // for all other types, take the value as is;
  // calling ODataUtils.formatValue would then even be wrong as it formats literals according to the
  // OData V2 standard
  this.getView().byId(“myPanel”).bindElement(
    “/ListB(“ + ODataUtils.formatValue(sItemBId, "Edm.String") + “)”);
}

If this does not help, please provide more details on what goes wrong, e.g. console log, requests you see in network tab, errors thrown, ...?

apazureck commented 7 years ago

Hi,

Thank you for your support. I just wanted to give you feedback, that I am currently not on this issue. Sorry. I solved it using navigation properties. But I think I had a misconception on where the binding on the odata model ends (and therefore the request sent to the serivce) and the "local" binding expression on the received data starts.

But as I stated earlier: It would be great, if you could provide some tutorial on this issue, as the current information on this topic is really hard to get as a newbie to ui5.

And having two odata services on one application (may be not Fiori conform) is not an exceptional issue, I guess. And I if you do not own the odata service it would be nice to have a little more light on how to combine the data from different sources.

Cheers