如何在UI5中动态创建实体路径? [英] How to Create Entity Path Dynamically in UI5?
问题描述
我想更新一个用OData svc填充的表.我正在使用这种方法:
I want to update a table populated with OData svc. I am using this approach:
oModel.update("/Products(999)", oData, {
success: mySuccessHandler,
error: myErrorHandler
});
我在变量中有选定的索引,我需要传递该变量.问题是Products(999)
-这适用于硬编码行,但是如何用变量替换?
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?
推荐答案
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 /*,...*/);
与手动连接路径的字符串相比,createKey
具有以下优点:
Compared to concatenating strings for the path manually, createKey
offers the following advantages:
- 它始终以与
ODataUtils.formatValue
内部). 例如::如果ProductID
具有类型Edm.Int64
,则UI5在→"999l"
- 确保所有键均根据URI标准进行编码(使用
encodeURIComponent
api ). 例如::ProductID='sp ace'
→ProductID='sp%20ace'
- 无论哪个后端系统提供元数据,它始终以正确的顺序输出键值 s .在给定相同的元数据定义的情况下,一个系统可能使用与其他系统不同顺序的键来提供元数据.在这种情况下,如果只是手动连接密钥,则在将应用程序传输到服务于不同密钥顺序的系统时,该应用程序将彻底抛出含糊不清的错误.
- 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.: IfProductID
has the typeEdm.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
encodeURIComponent
api 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.
由于createKey
依赖于服务元数据中的信息,因此应在加载$metadata
之后执行API.为此,基于承诺可以使用API metadataLoaded
.
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*/);
这篇关于如何在UI5中动态创建实体路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!