如何在 xml 中做到 "Navigation Property binding of aggregations"

How to do "Navigation Property binding of aggregations" in xml

我正在开发 UI5 应用程序。

我有以下 odata 实体:

<EntityType Name="order">
    <Key>
        <PropertyRef Name="id"/>
    </Key>
    <Property Name="id" Nullable="false" Type="Edm.Guid"/>
    <Property Name="creatorId" Type="Edm.Guid"/>
    <NavigationProperty FromRole="To_Order" Name="Creator" Relationship="bwm.UserOrder" ToRole="From_User"/>
</EntityType>
<EntityType Name="User">
    <Key>
        <PropertyRef Name="id"/>
    </Key>
    <Property Name="id" Nullable="false" Type="Edm.Guid"/>
    <Property Name="name" Type="Edm.String"/>
    <Property Name="phone" Type="Edm.String"/>
    <Property Name="avatar" Type="Edm.String"/>
    <NavigationProperty FromRole="From_User" Name="OrderSet" Relationship="bwm.UserOrder" ToRole="To_Order"/>
</EntityType>

我已经简化了实体模型。基本上有 OrderUser 实体,在 Order 实体上,有一个 导航 属性 Creator 引用User实体。

我想定义一个显示用户信息的片段,我想将 UI 元素相对于用户绑定 :

<HBox  xmlns="sap.m">
    <items>
        <Image src="{avatar}"/>
        <VBox>
            <items>
                <Label text="{name}"/>
                <Label text="{phone}"/>
            </items>
        </VBox>
    </items>
</HBox>

现在我有了显示订单列表的页面,但我不知道如何将元素绑定到 Creator 导航 属性:

<GrowingList threshold="10"
            items="{
                path: '/OrderSet'
            }">
    <items>
        <CustomListItem >
            <content>
                <core:Fragment fragmentName="xxx.fragment.User" type="XML"/>
            </content>
        </CustomListItem >
    </items>
</GrowingList>

如何设置fragment到导航的绑定路径属性Creator?

============================================= ================
PS:我四处搜索并找到了一些解决方案,这些解决方案不是我想要的,因为它将片段的数据绑定紧密地绑定到数据模型,解决方案的工作原理:

首先,绑定items路径时,展开Creator导航属性:

<GrowingList threshold="10"
            items="{
                path: '/OrderSet',
                paramters: {expand: 'Creator'}
            }">
    <items>
        <CustomListItem >
            <content>
                <core:Fragment fragmentName="xxx.fragment.User" type="XML"/>
            </content>
        </CustomListItem >
    </items>
</GrowingList>

其次,将片段的数据绑定更改为Creator/xxx:

 <HBox  xmlns="sap.m">
    <items>
        <Image src="{Creator/avatar}"/>
        <VBox>
            <items>
                <Label text="{Creator/name}"/>
                <Label text="{Creator/phone}"/>
            </items>
        </VBox>
    </items>
</HBox>

如果我想在显示用户列表的页面中使用它,这会使片段无用,因为在 UserSet 中,User 没有 属性 Creator/name。我想要的是通用的片段,只要根绑定路径是用户实体实例,它就可以在任何地方使用。

您可以给您的 HBox 一个标识符,并使用 bindElement 将其绑定到特定实体,具体取决于它是否是 Creator。通过这样做,您可以像在第一个片段中一样使用绑定。

更新

更简洁的实现方式是使用 new XML Templating syntax,将在 1.30.x 中介绍。请参阅下面使用的 <template:with> 标签。

<mvc:View
    xmlns="sap.m"
    xmlns:core="sap.ui.core"
    xmlns:form="sap.ui.layout.form"
    xmlns:mvc="sap.ui.core.mvc"
    xmlns:template="http://schemas.sap.com/sapui5/extension/sap.ui.core.template/1">

  <!-- "meta" model's binding context MUST point to an entity type -->
  <template:with path="meta>com.sap.vocabularies.UI.v1.Badge" var="badge">
      <form:SimpleForm>
        <form:title>
            <core:Title text="{path: 'badge>HeadLine', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}"/>
        </form:title>

        <Label text="{path: 'badge>Title/Label', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}"/>
        <Text text="{path: 'badge>Title/Value', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}"/>

        <Label text="{path: 'badge>MainInfo/Label', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}"/>
        <template:with path="badge>MainInfo" var="field">
            <core:Fragment fragmentName="sap.ui.core.sample.ViewTemplate.tiny.Field" type="XML"/>
        </template:with>

        <Label text="{path: 'badge>SecondaryInfo/Label', formatter: 'sap.ui.model.odata.AnnotationHelper.format'}"/>
        <template:with path="badge>SecondaryInfo" var="field">
            <core:Fragment fragmentName="sap.ui.core.sample.ViewTemplate.tiny.Field" type="XML"/>
        </template:with>
      </form:SimpleForm>
  </template:with>
</mvc:View>

这更简洁,不会增加额外控件的开销来包装您的片段。


原版POST

我 运行 今天遇到了和你一样的问题。我希望 XML 片段在同一视图中具有不同的绑定上下文。

我发现你的 cross-post on SCN 有一个由 Maksim Rashchynski 提出的合理解决方案。

由于我们无法将 binding 属性 应用于 <core:Fragment /> 定义,因此将我的片段包装在 sap.m.Panel 中似乎可以解决问题。

<Panel binding="{ path: 'myModel>/Some/ObjectA' }">
  <core:Fragment fragmentName="my.fragment.Something" type="XML" />
</Panel>

<Panel binding="{ path: 'myModel>/Some/ObjectB' }">
  <core:Fragment fragmentName="my.fragment.Something" type="XML" />
</Panel>

在我的片段中,我有:

<Label text="asdf" />
<Text text="{myModel>Comments}" />

确保您没有在片段中使用绝对绑定。

For those unaware, relative binding does not use a / in the binding path. Relative binding: {Comments}, Absolute binding: {/Comments}.