SAPUI5 Metadata.xml:绑定两个实体

时间:2016-06-20 14:16:08

标签: javascript json xml metadata sapui5

我在一家公司实习,我必须使用SAPUI5框架在JS中编码。这对我来说是全新的,这就是我遵循http://sapui5.hana.ondemand.com教程的原因。但现在我遇到了问题,我在互联网上找不到任何建议,而且我不能总是要求我的同事帮助我,他们也有工作......

我必须与许多员工及其任务进行规划。我使用带有metadata.xml文件的模拟服务器:

<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
Version="1.0">
<edmx:DataServices
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
    m:DataServiceVersion="1.0">
    <Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
        xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
        xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="NorthwindModel">
        <EntityType Name="Employee">
            <Key>
                <PropertyRef Name="EmployeeID" />
            </Key>
            <Property xmlns:p8="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
                Name="EmployeeID" Type="Edm.Int32" Nullable="false"
                p8:StoreGeneratedPattern="Identity" />
            <Property Name="LastName" Type="Edm.String" Nullable="false"
                MaxLength="20" Unicode="true" FixedLength="false" />
            <Property Name="City" Type="Edm.String" Nullable="false"/>
            <Property Name="Team" Type="Edm.String" Nullable="false"/>
            <NavigationProperty Name="Appointments" Relationship="NorthwindModel.FK_Employees_Appointment"
                FromRole="Employees" ToRole="Appointments"/>
        </EntityType>
        <EntityType Name="Appointment">
            <Key>
                <PropertyRef Name="AppointmentID"/>
            </Key>
            <Property Name="AppointmentID" Type="Edm.Int32" Nullable="false"/>
            <Property Name="EmployeeID" Type="Edm.Int32" Nullable="false"/>
            <Property Name="start" Type="Edm.DateTime" Nullable="false"/>
            <Property Name="end" Type="Edm.DateTime" Nullable="false"/>
            <Property Name="title" Type="Edm.String" Nullable="false"/>
            <NavigationProperty Name="Employees" Relationship="NorthwindModel.FK_Employees_Appointment"
                ToRole="Employees" FromRole="Appointments"/>
        </EntityType>
        <Association Name="FK_Employees_Appointment">
            <End Type="Employee" Role="Employees" Multiplicity="1" />
            <End Type="Appointment" Role="Appointments" Multiplicity="*" />
            <ReferentialConstraint>
                <Principal Role="Employees">
                    <PropertyRef Name="EmployeeID" />
                </Principal>
                <Principal Role="Appointments">
                    <PropertyRef Name="EmployeeID" />
                </Principal>
            </ReferentialConstraint>
        </Association>
    </Schema>
    <Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
        xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
        xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="ODataWeb.Northwind.Model">
        <EntityContainer
            xmlns:p7="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
            Name="NorthwindEntities" p7:LazyLoadingEnabled="true"
            m:IsDefaultEntityContainer="true">
            <EntitySet Name="Employees" EntityType="NorthwindModel.Employee" />
            <EntitySet Name="Appointments" EntityType="NorthwindModel.Appointment"/>
            <AssociationSet Name="FK_Employees_Appointments" Association="NorthwindModel.FK_Employees_Appointment">
                <End Role="Employees" EntitySet="Employees"/>
                <End Role="Appointments" EntitySet="Appointments"/>
            </AssociationSet>
        </EntityContainer>
    </Schema>
</edmx:DataServices>

(对于标签AssociationSet和NavigationProperty,我这样做是因为我在我使用的OData服务上看到过,但我不明白它;我猜它会好的......)

现在是Appointments.json文件:

[
{
    "AppointmentID" : 0,
    "EmployeeID" : 0,
    "start":"\/Date(1466416800000)\/",
    "end":"\/Date(1466424000000)\/",
    "title": "test"
},{
    "AppointmentID" : 1,
    "EmployeeID" : 1,
    "start":"\/Date(1466409600000)\/",
    "end":"\/Date(1466416800000)\/",
    "title": "test2"
}]

Employees.json文件:

[
{
    "EmployeeID":0,
    "LastName":"APERCE",
    "City":"Paris",
    "Team":"Dev"
},
{
    "EmployeeID":1,
    "LastName":"HACHMI",
    "City":"Lille",
    "Team":"Dev"
},
{
    "EmployeeID":2,
    "LastName":"MILLET",
    "City":"Paris",
    "Team":"Admin"
},
{
    "EmployeeID":3,
    "LastName":"CORNET",
    "City":"Poitiers",
    "Team":"Admin"
},
{
    "EmployeeID":4,
    "LastName":"EVAIN",
    "City":"Paris",
    "Team":"R&D"
}]

(如您所见,EmployeeID出现在Appointments.json和Employees.json上)

最后是Overview.view.xml文件:

<mvc:View controllerName="sap.ui.demo.wt.controller.Overview"
xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:semantic="sap.m.semantic"
xmlns:unified="sap.ui.unified"
xmlns:core="sap.ui.core" displayBlock="true">
<semantic:FullscreenPage title="{i18n>overviewPageTitle}">
    <VBox>
        <PlanningCalendar startDate="{path : '/Startdate'}" rows="{path : 'data>/Employees',parameters:{expand:'Appointments'}}"
            appointmentSelect="handleAppointmentSelect">
            <rows>
                <PlanningCalendarRow title="{data>LastName}"
                    appointments="{path : 'data>Appointments',templateShareable:true}">
                    <appointments>
                        <unified:CalendarAppointment
                            startDate="{data>start}"
                            endDate="{data>end}"
                            title="{data>title}">

                        </unified:CalendarAppointment>
                    </appointments>
                </PlanningCalendarRow>
            </rows>
        </PlanningCalendar>
    </VBox>
</semantic:FullscreenPage>

(“data”模型已经与metadata.xml绑定并同时获取Appointments.json和Employees.json)

实际上,此代码显示了Appointments.json中针对我计划中每位员工的两项任务。我想绑定它,以便仅为“HACHMI”的员工“APERCE”和“test2”进行约会“测试”,但我不能。

我相信可以为SQL数据库执行等同于JOIN的操作,但我找不到任何关于它的信息。我该怎么办?谢谢你的回答。

PS:我是法国人,如果你不明白我上面写的那些,抱歉;如果你能用法语回答,那么这对我的理解会更好。

编辑:我已根据nistv4n的建议更正了代码。现在我收到以下错误:

MockServer:找不到该段'Appointment'的资源=&gt;据我所知,“预约”部分不存在,因为它是“约会 s ”,但我不知道我在哪里忘记了's'。

发生以下问题:HTTP请求失败404,未找到,{“错误”:{“code”:404,“message”:{“lang”:“en”,“value”:“找不到资源细分'约会'“}}} =&gt;我想这与之前的错误有关,因为它要求“约会”而不是“约会 s ”?

REEDIT:我把's'放在正确的位置。现在我只有:无法读取undefined =&gt;属性'getTime'因为在Overview.view.xml中使用的模型存在问题,可能在,但是如果我使用它是相同的:“{Appointments / start}”,“{Appointments&gt; start}”,“{start}”或“{/预约&GT;启动}”。

LASTEDIT:感谢nistv4n,我终于做到了。如果您遇到同样的问题,我已经更新了上面的代码。正确的代码在PlanningCalendarRow =&gt;中。 appointmentments =“{path:'data&gt; Appointments'}”并统一:CalendarAppointment =&gt; startDate =“{data&gt; start}”等...

实际上,引用nistv4n:“约会有一个相对引用,内部属性(开始,结束,标题)也有一个相对引用,包括模型名称。”

1 个答案:

答案 0 :(得分:1)

在为PlanningCalendar的聚合行定义绑定的位置,包含$expand关键字,以便在容器中访问引用的活动:

<PlanningCalendar startDate="{path : '/Startdate'}" rows="{path : 'data>/Employees', parameters:{expand:'Appointment'}}" 
appointmentSelect="handleAppointmentSelect">

它将获取给定员工的指定Appointment数据。您可以使用聚合内的Appointment/start绑定路径来引用它。

修改 您似乎错过了第一个架构的Association节点。像这样定义:

<Association Name="FK_Employees_Appointment">
    <End Type="Employee" Role="Employees" Multiplicity="1" />
    <End Type="Appointment" Role="Appointments" Multiplicity="*" />
    <ReferentialConstraint>
        <Principal Role="Employees">
            <PropertyRef Name="EmployeeID" />
        </Principal>
        <Principal Role="Appointments">
            <PropertyRef Name="EmployeeID" />
        </Principal>
    </ReferentialConstraint>
</Association>

我建议只定义两个实体之间的单向连接。

您可以将代码发布到某个地方(甚至是zip存档中)来调查整个架构和代码行吗?

编辑2:请根据以下内容更改View XML的PlanningCalendarRow段:

<PlanningCalendarRow title="{data>LastName}"
    appointments="{path : 'data>Appointments',templateShareable:true}">
    <appointments>
        <unified:CalendarAppointment
            startDate="{data>start}"
            endDate="{data>end}"
            title="{data>title}">

        </unified:CalendarAppointment>
    </appointments>
</PlanningCalendarRow>

Appointments有一个相对引用,内部属性(startendtitle)也有一个相对引用,包括模型名称。