是否可以在一个视图中使用 2 个模型 [英] Is it possible to use 2 models in one view

查看:35
本文介绍了是否可以在一个视图中使用 2 个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在思考 SAPUI5 中是否可能存在一个概念,并且由于文档在这种详细级别上可能有点难以处理,因此我在研究路径中比正常情况更早地提出了这个问题.主要是我不想花太多时间,如果有一个直接的否"回答.

I am stuck on whether a concept is possible in SAPUI5, and as the docs can be a little hard to work with at this level of detail, I am asking this question earlier in my research path than normal. Mainly I don't want to spend too much time if there is an immediate 'No' answer.

我有一个使用 JSON 模型的主从视图用例,但我必须生成自己的控件 - 我不能使用 SplitApp 等.

I have a use case of a view that is master-detail, using a JSON model, but I have to produce my own controls - I can't use a SplitApp etc.

该模型实际上是一个 2 层深的树,主通向细节.概念上是这样的:

The model is effectively a 2-layer deep tree, master leading to detail. Conceptually like this:

{ 
    "master": [
        {
            "name": "Master 1",
            "detail" : [
                {
                "name": "Detail 1-1"
                },
                {
                "name": "Detail 1-2"
                }
            ]
        },
        {
            "name": "Master 2",
            "detail" : [
                {
                "name": "Detail 2-1"
                },
                {
                "name": "Detail 2-2"
                }
            ]
        }
    ]
}   

用户体验是将有一个可选择的母版列表,当进行选择时,详细信息列表控件将刷新以显示作为所选母版的子级的详细信息.

The UX is that there will be a selectable list of masters and when a selection is made the detail list control will be refreshed to show the details that are children of the selected master.

我熟悉通过

sap.ui.getCore().setModel(oModel) 
this.getView().setModel(oModel)

但是根据我迄今为止的学习,这会将视图中的所有控件绑定到一个模型.

but this binds all controls in the view to one model according to my learning to date.

我认为可能值得追求的选项是:

The options I think may be worth pursuing are:

  1. 将单独的模型绑定到每个主控件和细节控件,并编写自定义代码以在选择主控件时切换细节模型.但是如何进行这样的绑定;
  2. 使用单个模型,但以某种方式设置细节控件以识别当前选定的"母版.
  3. 在细节控件上使用某种过滤器.

进一步解释 2,如果说我正在使用一个表格来显示我可能在表格 def 中的详细信息

Explaining 2 further, if say I was using a table for the detail display I might have in the table def

   <Table
            id="detailList"
            items="{
                path: '/'
            }",
            ...
     >

所以我希望将路径修改为类似

So I would be looking to modify the path to something like

path: '/master[n]/detail/'

其中 n 代表选定的主实例.

where n represents the selected master instance.

要么有可能,要么有其他选择,或者我应该放弃.

Is either possible, or is there another alternative, or should I give up.

我通过 Michael Herzog here 发现了这个潜在的基于过滤器的解决方案.他的描述是:

I found this potential filter-based solution by Michael Herzog here. His description is:

我会给你一个例子,你可以如何实现一个 matser-detailSAPUI5 中的关系.

i'll give you an example, how you can implement a matser-detail relationship in SAPUI5.

用例:当用户点击第一个表格中的客户端时,所有相关订单都应显示在第二个表中.命令应该对其他客户端隐藏.

Use case: When the user is clicking on a client in the first table, all related orders should be displayed in the second table. Orders from other clients should be hidden.

因此,在我们看来,我们有两个表:

So, in our view, we have two tables:

  1. 表格:客户

  1. Table: Clients

表格:订单

创建两个表并设置数据绑定后,执行第一个表上的这个事件处理程序:

After you created both tables and set up the databinding, implement this eventhandler on the first table:

oTableClients.attachRowSelectionChange( function(oEvent){

    // first, we fetch the binding context of the selected row
     var selectedRowContext = oEvent.getParameter("rowContext");
     // get the ID of the customer via rowContext. The model-object represents the data of the first table
     var selectedClientId = oModel,getProperty("id", selectedRowContext);
     // get binding of second table
     var ordersBinding = oTableOrders.getBinding();
     //create new filter to show the relevant data for the selected customer
     var oFilter = new sap.ui.model.Filter("clientId", sap.ui.model.FilterOperation.EQ, selectedClientId);
     // apply filter to binding
     ordersBinding.filter([oFilter]);
});

我可以看出这种方法对我来说是可行的 - 有什么理由说明这种模式存在根本性缺陷吗?

I can see that this approach is viable for me - is there any reason why this pattern is fundamentally flawed?

推荐答案

一般来说,您的视图可以拥有您喜欢的任意数量的模型.唯一的限制是只能有一个无名模型,即所有其他模型都需要命名.以下代码显示了差异:

In general your view can have as much models you like. The only restriction is that there can be only one nameless model, i.e. all other models need to be named. The following code shows the difference:

this.getView().setModel(new JSONModel(data));
this.getView().setModel(new JSONModel(data), name));

要绑定命名模型,您必须在绑定指令中提供其名称,否则运行时将使用无名模型.以下示例显示了差异(请注意,除非您想提供其他参数,否则可以使用短绑定语法,即可以省略 path 属性:

To bind against a named model you have to provide its name in the binding instruction, otherwise the runtime uses the nameless model. The following example shows the difference (note that you can use the short binding syntax unless you want to provide additional parameters, i.e. you can omit the path attribute:

<Table items="{/path}">
<Table items="{name>/path}">

在您的示例中,我建议使用一个模型并使用绑定上下文来控制详细信息表中显示的数据.

In your example I would suggest to work with one model and use the binding context to control the data shown in the detail table.

明细表的绑定应该是相对的,如下所示:

The binding of the details table should be relative and look as follows:

<Table id="detailList" items="{detail}">

用于处理主列表选择的处理程序如下:

The handler for handling selection of a master list as follows:

onMasterItemSelect : function(event) {
    // get the binding context of the currently selected master item, e.g. /master/0
    var masterBindingContext = event.getParameter("listItem").getBindingContext();

    // bind detail table to the selected master item using bindElement
    this.byId("details").bindElement(masterBindingContext.getPath());
}

这篇关于是否可以在一个视图中使用 2 个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆