Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

odata - How to Create Entity Path Dynamically in UI5?

I want to update a table populated with OData svc. I am using this approach:

oModel.update("/Products(999)", oData, {
  success: mySuccessHandler,
  error: myErrorHandler
});

I have the selected index in a variable and I need to pass that variable. The problem is Products(999) - this is working with the hard coded row but how to replace with a variable?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Create the path dynamically via the API createKey from the ODataModel:

const path = myODataModel.createKey("/Products", {
  ProductID: 999, // your dynamic key value
  AnotherKeyProperty: "...",
});
myODataModel.update(path /*,...*/);

Compared to concatenating strings for the path manually, createKey offers the following advantages:

  • It outputs the key value always in the correct format corresponding to the EDM type of the given property (using ODataUtils.formatValue internally). E.g.: If ProductID has the type Edm.Int64, UI5 appends the character "l" in the output string aligning with the OData specification: "999""999l"
  • It makes sure that all keys are encoded according to the URI standard (using encodeURIComponentapi internally). E.g.: ProductID='sp ace'ProductID='sp%20ace'
  • It outputs the key values always in the correct order no matter which backend system serves the metadata. Given the same metadata definition, it is possible that one system serves the metadata with keys in different orders than others. In such cases, if keys are just concatenated manually, the app would fail horribly throwing vague errors when it's transported to the system serving different key orders.

Note

Since createKey relies on the information from the service metadata, the API should be performed after $metadata is loaded. For this, the promise based API metadataLoaded can be used.

myODataModel.metadataLoaded().then(/*createKey*/);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...