什么是 parts: 和 path: 在 sapui5 中,为什么使用它们?

what are parts: and path: in sapui5 and why are they used?

SAPUI5中处理模型时partspath有什么用?

有人可以就以下代码(其中 invoice 是 JSONModel)向我解释一下吗?

<mvc:View
    controllerName="sap.ui.demo.walkthrough.controller.InvoiceList"
    xmlns="sap.m"
    xmlns:mvc="sap.ui.core.mvc">
    <List
            headerText="{i18n>invoiceListTitle}"
        class="sapUiResponsiveMargin"
        width="auto"
        items="{invoice>/Invoices}">
        <items>
            <ObjectListItem
                title="{invoice>Quantity} x {invoice>ProductName}"
                number="{
                    parts: [{path: 'invoice>ExtendedPrice'}, {path: 'view>/currency'}],
                    type: 'sap.ui.model.type.Currency',
                    formatOptions: {
                        showMeasure: false
                    }
                }"
                numberUnit="{view>/currency}"
                numberState="{= ${invoice>ExtendedPrice} > 50 ? 'Error' : 'Success' }">
                <firstStatus>
                    <ObjectStatus text="{
                        path: 'invoice>Status',
                        formatter: '.formatter.statusText'
                    }"/>
                </firstStatus>
            </ObjectListItem>

        </items>
    </List>
</mvc:View>

简单来说,在path内只能绑定一个属性或value,而parts可以让你绑定多个属性或值,这有时非常有用。

在你的例子中,货币格式需要两个参数,一个是金额,一个是货币,你必须使用零件来解析两个参数。

另外当你自己写formatter函数的时候,如果你想要更多的参数,你也可以使用parts来接收这些参数。

这称为复合绑定

此致, 马文

欢迎使用 Whosebug!

您引用的是 binding。在您的示例中,您有:

  • 列表绑定:items="{invoice>/Invoices}".
  • 简单 属性 绑定:numberUnit="{view>/currency}".
  • 复合 属性 绑定:number="{parts: [...]}(使用显式语法)和 title="{invoice>Quantity} x {invoice>ProductName}"(使用复杂语法)。
  • 表达式绑定:numberState="{= ${invoice>ExtendedPrice} > 50 ? 'Error' : 'Success' }"

SDK 在 Data binding.

章下有一些关于这些主题的详尽文档页面

列表绑定 用于根据模型内部的数据(基于列表或对象映射)创建控件集合。从概念上讲,您可以想象 UI5 循环遍历您的值并使用模板或工厂函数实例化相应的控件。 path 在这种情况下是指向集合的(相对或绝对)路径。

简单 属性 绑定 仅用于根据模型中的单个标量字段填充模型中的控件 属性。这里的 path 是通向 属性.

的(相对或绝对)路径

复合属性绑定可用于填充属性基于多个字段的控件,这些字段通过格式化程序函数或一种类型(如您示例中的货币)。例如,当使用格式化程序时,每个 part 都将作为参数传递给您的函数(例如,如果您有 2 个部分,则您的格式化程序应该期望有 2 个参数)。此处的 部分 用于定义计算 属性 值时要使用的每个单独字段。

表达式绑定或复杂语法只是一种语法糖,允许您定义内联格式化程序,而无需编写专门的 JS 函数。

你总是可以使用简化语法property="{/path}"或扩展语法property="{path: '/path'}",它们是等价的(但是一旦你想指定更多的绑定参数,你就必须使用扩展语法)。